由于在工厂中使用了“序列",如何避免使用"FactoryGirl.reload"? [英] How can I avoid using 'FactoryGirl.reload' due to using 'sequence' in my factories?
问题描述
必须使用FactoryGirl.reload
可能会在运行所有测试的时间(存在许多测试)上增加一些开销,并且也被描述为
Having to use FactoryGirl.reload
likely to put some overhead on the time it takes to run all tests (when many tests exist) and also it is described as an Anti-Pattern.
我如何继续对唯一字段使用排序,但要测试正确的值?
How can I keep using the sequencing for unique fields, yet test for the correct values?
您可以在我的代码中看到...
You can see in my code that...
-
我必须呼叫
FactoryGirl.reload
,以便从1
开始递增,以防万一以前的测试已经运行.
I have to call
FactoryGirl.reload
so that the increment starts at1
in case any previous tests have already been run.
我必须声明:count => 1
,因为将永远只有该值的一个实例.
I have to state :count => 1
because there will only ever be one instance of that value.
spec/factories/clients.rb
FactoryGirl.define do
factory :client do
name "Name"
sequence(:email} { |n| "email#{n}@example.com" }
sequence(:username) { |n| "username#{n}" }
end
end
spec/clients/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
推荐答案
如何为此特定测试传递特殊名称?
How about passing in special names for this particular test?
require 'spec_helper'
describe "clients/index" do
before do
(1..5).each do |num|
FactoryGirl.create(:client, username: "special#{num}")
end
@clients = Client.all
end
it "renders a list of clients" do
render
assert_select "tr>td", :text => "special1".to_s, :count => 1
end
end
这篇关于由于在工厂中使用了“序列",如何避免使用"FactoryGirl.reload"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!