我一直试图让 FactoryGirl 关联在我的控制器中工作。
保存了许多“想法”实例,这导致“将所有想法分配为@ideas”规范失败。我不确定究竟是为什么。对于此设置,我不希望只有一个实例保留在数据库中吗?如果我一次又一次地运行规范,额外实例的数量每次都会翻倍。我的理解是使用“Idea.create!” 在我的规范中,如下所示将为测试创建一个对象,然后将其从数据库中删除以供下次运行。
我有一个独特的电子邮件序列,使用纳秒来避免重复的电子邮件。使用 FactoryGirl 序列计数器时,我不断收到电子邮件已存在错误,这可能与此问题直接相关。我怎样才能更有效地解决这个问题?
摘自 /controllers/ideas_controller_spec.rb:
valid_attributes = FactoryGirl.attributes_for(:idea)
describe "GET index" do
it "assigns all ideas as @ideas" do
idea = Idea.create! valid_attributes
get :index, {}, valid_session
assigns(:ideas).should eq([idea])
end
end
工厂/idea.rb:
FactoryGirl.define do
factory :idea do
brief 'Valid brief for an idea'
phase 1
active true
industry 'Technology'
user
end
end
工厂/用户.rb
FactoryGirl.define do
factory :user do
sequence(:email) {
|n| time = Time.new
"person#{time.usec}@example.com"
}
password '12345678'
password_confirmation '12345678'
end
end
规范输出(在运行“bundle exec rake db:test:prepare”后立即):
1) IdeasController GET index assigns all ideas as @ideas
Failure/Error: assigns(:ideas).should eq([idea])
expected: [#<Idea id: 6, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: nil>]
got: #<ActiveRecord::Relation [#<Idea id: 1, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 3>, #<Idea id: 2, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 5>, #<Idea id: 3, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 9>, #<Idea id: 4, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 11>, #<Idea id: 5, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 14>, #<Idea id: 6, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: nil>]>
(compared using ==)
Diff:
@@ -1,2 +1,7 @@
-[#<Idea id: 6, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: nil>]
+[#<Idea id: 1, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 3>,
+ #<Idea id: 2, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 5>,
+ #<Idea id: 3, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 9>,
+ #<Idea id: 4, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 11>,
+ #<Idea id: 5, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 14>,
+ #<Idea id: 6, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: nil>]
# ./spec/controllers/ideas_controller_spec.rb:37:in `block (3 levels) in <top (required)>'