Reputation: 1643
I have a failing test where, to my understanding, it should be creating objects with unique values by using the Faker gem. I'm really stuck here. Thanks for the help!
Failing test from user_spec.rb:
describe "post associations" do
before { @user.save }
let!(:older_post) do
older_post = FactoryGirl.create(:post, user: @user, created_at: 1.day.ago)
older_post.add_tags!([Faker::Lorem.words(1), Faker::Lorem.words(1), Faker::Lorem.words(1)])
end
let!(:newer_post) do
newer_post = FactoryGirl.create(:post, user: @user, created_at: 1.hour.ago)
newer_post.add_tags!([Faker::Lorem.words(1), Faker::Lorem.words(1), Faker::Lorem.words(1)])
end
it "should have the right posts in the right order" do
@user.posts.should == [newer_post, older_post]
end
end
Post factory:
factory :post do
title Faker::Lorem.sentence(Random.rand(1..5))
content Faker::Lorem.paragraphs(3)
shared_url Faker::Internet.url
public_post true
user
end
Rspec results:
1) User post associations should have the right posts in the right order
Failure/Error: @user.posts.should == [newer_post, older_post]
expected: [true, true]
got: [#<Post id: 2, title: "Sapiente dignissimos qui et a.", content: "---\n- Quis necessitatibus eligendi sunt distinctio ...", shared_url: "http://blanda.biz/xander.cormier", public_post: true, created_at: "2013-06-28 15:31:00", updated_at: "2013-06-28 16:31:00", user_id: 1>, #<Post id: 1, title: "Sapiente dignissimos qui et a.", content: "---\n- Quis necessitatibus eligendi sunt distinctio ...", shared_url: "http://blanda.biz/xander.cormier", public_post: true, created_at: "2013-06-27 16:31:00", updated_at: "2013-06-28 16:31:00", user_id: 1>] (using ==)
Diff:
@@ -1,2 +1,3 @@
-[true, true]
+[#<Post id: 2, title: "Sapiente dignissimos qui et a.", content: "---\n- Quis necessitatibus eligendi sunt distinctio ...", shared_url: "http://blanda.biz/xander.cormier", public_post: true, created_at: "2013-06-28 15:31:00", updated_at: "2013-06-28 16:31:00", user_id: 1>,
+ #<Post id: 1, title: "Sapiente dignissimos qui et a.", content: "---\n- Quis necessitatibus eligendi sunt distinctio ...", shared_url: "http://blanda.biz/xander.cormier", public_post: true, created_at: "2013-06-27 16:31:00", updated_at: "2013-06-28 16:31:00", user_id: 1>]
# ./spec/models/user_spec.rb:94:in `block (3 levels) in <top (required)>'
Upvotes: 0
Views: 356
Reputation: 124419
In each of your :let!
blocks, the last line evaluated is the add_tags!
method, which appears to be returning true
instead of the post the tags were added to. Because of this, in your spec [newer_post, older_post]
actually equals [true, true]
instead of your actual posts.
At the end of each :let!
block, return the post:
let!(:older_post) do
older_post = FactoryGirl.create(:post, user: @user, created_at: 1.day.ago)
older_post.add_tags!([Faker::Lorem.words(1), Faker::Lorem.words(1), Faker::Lorem.words(1)])
older_post
end
let!(:newer_post) do
newer_post = FactoryGirl.create(:post, user: @user, created_at: 1.hour.ago)
newer_post.add_tags!([Faker::Lorem.words(1), Faker::Lorem.words(1), Faker::Lorem.words(1)])
newer_post
end
Alternatively, you could change your add_tags!
method to return the post instead of true
/false
.
Upvotes: 1