必须使用FactoryGirl.reload
可能会增加运行所有测试所需的时间(当存在许多测试时),并且它也被描述为Anti-Pattern。
如何继续使用唯一字段的排序,同时测试正确的值?
你可以在我的代码中看到...
我必须打电话
FactoryGirl.reload
,以便增量开始,1
以防任何先前的测试已经运行。我必须说明,
:count => 1
因为该值只会有一个实例。
规格/工厂/clients.rb
FactoryGirl.define do
factory :client do
name "Name"
sequence(:email} { |n| "email#{n}@example.com" }
sequence(:username) { |n| "username#{n}" }
end
end
规范/客户端/index.html.erb_spec.rb
require 'spec_helper'
describe "clients/index" do
before do
FactoryGirl.reload
(1..5).each do
FactoryGirl.create(:client)
end
@clients = Client.all
end
it "renders a list of clients" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "tr>td", :text => "Name".to_s, :count => 5
assert_select "tr>td", :text => "email1@example.com".to_s, :count => 1
assert_select "tr>td", :text => "username1".to_s, :count => 1
end
end