I am stuck trying to get FactoryGirl associations to work in my controller.
A number of instances of 'Idea' are saved, and this causes the 'assigns all ideas as @ideas' spec to fail. I am not sure exactly why. For this setup, wouldn't I want only one instance to remain in the database? If I run the spec again and again the number of extra instances doubles each time. My understanding was that using "Idea.create!" in my spec as shown below would create an object for the test and then remove it from the database for the next run.
I have a unique email sequence using nanoseconds to avoid duplicate emails. I kept getting email already exists errors when using the FactoryGirl sequence counter, which could be directly related to this issue. How might I go about troubleshooting this more effectively?
Excerpt from /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
factories/idea.rb:
FactoryGirl.define do
factory :idea do
brief 'Valid brief for an idea'
phase 1
active true
industry 'Technology'
user
end
end
factories/user.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
Spec output (immediately after running "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)>'
I would guess that you have transactions turned off for rspec, which would result in the database growing as you have observed.
Note that you also have an issue in that as of Rails 4.0, ActiveRecord
is returning a Relation
instead of an Array
, so you need to use the to_a
method prior to comparing it to an Array
.