bcreate the data quickly and easily。考虑测试运行的速度。

fixtures and factories.以及下章讨论的test doubles,还有原生的create创建的,没有一个方案可以解决所有情景。


Fxitures

如果想使用固件。RSpec目录是spec/fixtures/projects.yml。 Mini test的目录是test/fixtures/

runway:
   name: Project Runway

due_date: 2016-12-18

projects(:runway)

名字可以描述用途,比如projects(:project_with_no_due_date)

Fixture接受ERB files.可以使用<%= %> ,不建议使用使用循环。

Loading Fixture Data

在测试开始时固件会一次性加载,使用事物transaction,因此在测试结束时会回滚。回归初始状态。

固件运行速度块。固件是全局性的数据 储存在database中。

固件是分散的,不适合管理connections,关联数据。

总之,固件不适合在复杂的程序使用。


Factories   factory_bot

gem 'factory_bot_rails'  目录spec/factories/

在spec的一个文件中配置:

RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end

每次用create方法无需加上FactoryBot前缀了。

详细用法:本博客: https://www.cnblogs.com/chentianwei/p/9047596.html

Factory_bot假定class的名字就是你定义的名字。如果要自定义类,需要指定类名。

FactoryBot.define do
  factory :project, class: Project do
    name "Project Runway"
    due_date {Date.today - rand(50)} #随机时间。50天内的。
  end
end

Basic Factory Creation

build(:project): 非关联对象可以使用。

create(:project):别用,除非对象之间有关联,必须存入数据库。因为会让测试减慢。

attributes_for(:project)   需要对hash属性进行验证时,使用,一般见于controller test。

build_stubbed(:project) Unlike build, it assigns a fake ActiveRecord
ID to the model and stubs out database-interaction methods (like save)
such that the test raises an exception if they are called.

Prescription: 尽量使用这个方法,有build的一切功能,还能生成一个假Rails id,有了这个就能建立belongs_to 关联,同时不会被存入数据库。

*_pair  and *_list 两个方法,没看。

Associations and Factories 

FactoryBot.define do
  factory :task do
    title "Thing to do"
    size 1
    completed_at nil
    project   

#这样调用task = create(:task)就会自动调用task.project = create(:project)

  end
end

也可以单独建立关联:

task
= FactoryBot.create(:task,  project: Project.new)

如果已经在factory definition中建立了关联,同时不想在测试中有value,设置nil

task = FactoryBot.create(:task,
project: nil).

如果关联名字不匹配factory name或者你想指定关联对象的默认的属性:

在factory definition 中使用association方法

association :doer,  factory: :user,  name: "Task Doer"

只有保存在数据库中,对象有了id才能关联。但可以使用build_stubbed来创建不保存的对象

或者使用create方法。

作者建议在定义中建立关联,但不设置属性。属性在测试中设置。

association :user

更直接的 user

Managing Duplication in Factories 

对于2个以上的factories, Factory_bot可以做到管理。文档里面有许多技巧,但以下3个最常用。

sequence方法 

对于需要unique values 的属性,如登陆,或电子邮件。可以使用sequence方法。

sequence(:title) { |n| "Task #{n}"}

起始n是1. 每被调用一次n加1。

inherited factories 继承的特性

FactoryBot.define do
  factory :task do
    sequence(:title){ |n| "Task #{n}"}  
#可以嵌套进来
    factory :big_task do
      size 5
    end
    factory :small_task do
      size 1
    end
  end
#或者加上parent: :task
  factory :middle_task, parent: :task do
    size 3
  end
end

trait方法

trait

优点:设置一个有意义的名字,方便理解

缺点:需要额外的输入和增加了复杂度。

用于指定一批的特定的属性,一个省事的方法。

这个方法放在factory :task的块中,就可以被task使用。

FactoryBot.define do
  factory :task do
    title "Thing to do"
    size 1
    completed_at nil
    trait :small do
      size 1
    end

#继承一个对象,并使用了trait

    factory :trivial do
      small
    end
  end
end

调用: let(:task){build(:task, :smal)}

Preventing Factory Abuse

创建最小数量的数据刚好够测试用就可以了。


Dates and Times

测试历法逻辑,让人头疼,但可以使用a couple of things 来简化时间逻辑这个野兽 .

Using Relative Dates

Date.today 和 1.day.ago, 2.months.ago, 2.weeks.ago结合使用。

如在

factory :project do

start_date {1.week.ago}

end

gutenberg:

start_date: <%= 1.day.ago %>

缺点就是如果某个时间是已知的,就没办法使用相对时间了。

Stubbing Time

把时间冻结!!!用a stub来明确指定时间。

需要用到Raisl helper方法。

ActiveSupport::Testing::TimeHelpers#travel

Changes current time to the time in the future or in the past by a given time difference by stubbing Time.now, Date.today, and DateTime.now. The stubs are automatically removed at the end of the test.

travel(duration, &block)

Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
travel 1.day do
User.create.created_at # => Sun, 10 Nov 2013 15:34:49 EST -05:00
end
Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00

如 travel 1.month, 接受一段时间作为参数。 一个整数。

ActiveSupport::Testing::TimeHelpers#travel_to

travel_to(date_or_time)

接受1个固定的时间/日记,作为参数。

pasting

Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
travel_to Time.zone.local(2004, 11, 24, 01, 04, 44) do
Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00
end
Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00

trave_back 重置时间回到初始状态。

可以在这两个参数的块内,进行断言,或者期望expect.

Comparing Time

Ruby有三个独立的时间类。Time, Date, DateTime.

因此Date的实例和DateTime的实例是无法比较和加减的

使用to_s(:db)进行转化。

 1.day.ago.to_date.to_s(:db)  => "2018-05-25"

 1.day.ago.to_date   => Fri, 25 May 2018

Setting Rails Timestamps

可以使用created_at 在预制件,或者固件中。

Rails 5 Test Prescriptions 第6章Adding Data to Tests的更多相关文章

  1. Rails 5 Test Prescriptions 第8章 Integration Testing with Capybara and Cucumber

    Capybara:  A complete reference is available atrubydoc.info. 集成测试就是把局部的程序组合起来测试. 端到端测试是一个特殊的集成测试,覆盖了 ...

  2. Rails 5 Test Prescriptions 第3章Test-Driven Rails

    本章,你将扩大你的模型测试,测试整个Rails栈的逻辑(从请求到回复,使用端到端测试). 使用Capybara来帮助写end-to-end 测试. 好的测试风格,包括端到端测试,大量目标明确的单元测试 ...

  3. Rails 5 Test Prescriptions 第10章 Testing for Security

    Web 安全是一个可怕的主题.所有的你的程序都依靠密码学,代码超出了你的控制. 尽管如此,你还是可以控制部分网页安全 --所有的logins和access checks和injection error ...

  4. Rails 5 Test Prescriptions 第11章其他部分的测试。

    Routes✅ Helper Methods✅ Controllers and Requests✅ Simulating Requests⚠️,看之前的博客 What to Expect in a R ...

  5. Rails 5 Test Prescriptions 第9章 Testing-JavaScript: Integration Testing,❌挂一个问题webpacker::helper

    使用Capybara进行JS的集成测试 谈论驱动 让测试通过 Webpack in Development Mode Js设计 是用户在网页上有好的体验的重要因素. 尽管如此,许多网页不测试JS. 部 ...

  6. Rails 5 Test Prescriptions 第5章 Testing Models

    Rails,model层包含业务逻辑和储存逻辑.其中储存逻辑被ActiveRecord处理. 在model中,不是每件事都必须是ActiveRecord对象.model layer可以包含各种服务,对 ...

  7. Rails 5 Test Prescriptions 第4章 什么制造了伟大的测试

    伴随着程序成长,测试变长,复杂性增加,如何更高效的写测试,对以后开发不会造成麻烦. 测试本身没发被测试,所以一定要清楚,可控.不要加循环,不要过于复杂的自动编程. Cost and Value 成本和 ...

  8. Rails 5 Test Prescriptions 第10章 Unit_Testing JavaScript(新工具,learn曲线太陡峭,pass)

    对Js的单元测试是一个大的题目.作者认为Ruby的相关测试工具比Js的测试工具更灵活 大多数Js代码最终是关于响应用户的行为和改变DOM中的元素 没有什么javascript的知识点.前两节用了几个新 ...

  9. Rails 5 Test Prescriptions 第7章 double stub mock

    https://relishapp.com/rspec/rspec-mocks/v/3-7/docs/basics/test-doubles 你有一个问题,如果想为程序添加一个信用卡程序用于自己挣钱. ...

随机推荐

  1. Apache的配置详解,最好的Apache配置文档

    http://blog.csdn.net/apple_llb/article/details/50253889 Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.c ...

  2. Golang OOP、继承、组合、接口

    http://www.cnblogs.com/jasonxuli/p/6836399.html   传统 OOP 概念   OOP(面向对象编程)是对真实世界的一种抽象思维方式,可以在更高的层次上对所 ...

  3. centos Linux系统日常管理1 cpuinfo cpu核数 命令 w, vmstat, uptime ,top ,kill ,ps ,free,netstat ,sar, ulimit ,lsof ,pidof 第十四节课

    centos Linux系统日常管理1  cpuinfo cpu核数   命令 w, vmstat, uptime ,top ,kill ,ps ,free,netstat ,sar, ulimit ...

  4. PAT 1090 Highest Price in Supply Chain[较简单]

    1090 Highest Price in Supply Chain(25 分) A supply chain is a network of retailers(零售商), distributors ...

  5. 统计编程的框架与R语言统计分析基础——摘(2)统计分析之线性回归

    一.线性回归 1.简单线性回归 a. > x = women > x height weight 1 58 115 2 59 117 3 60 120 4 61 123 5 62 126 ...

  6. pip 解决 ImportError: cannot import name 'main'

    当 pip 更新至最新版的时候,不管是执行 pip list 还说 pip install packageName 安装包,都会抛出一个异常 Traceback (most recent call l ...

  7. html5 七巧板

    <!DOCTYPE html><html>    <canvas id="diag" height="200" width=&qu ...

  8. 2017-2018 ACM-ICPC Latin American Regional Programming Contest Solution

    A - Arranging tiles 留坑. B - Buggy ICPC 题意:给出一个字符串,然后有两条规则,如果打出一个辅音字母,直接接在原字符串后面,如果打出一个元音字母,那么接在原来的字符 ...

  9. C Strange Sorting

    C. Strange Sorting time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  10. 使用idea 搭建Spring+mybatis

    1.file-new-project 项目的结构如下: 在WEB-INF 下面新建一个 文件夹lib 右键WEB-INF ,new-Directory 所需要的jar 包有: lib下载地址: 网盘地 ...