0
我有一個失敗的測試,根據我的理解,它應該使用Faker gem創建具有唯一值的對象。我真的被困在這裏。謝謝您的幫助!FactoryGirl沒有創建獨特的對象
從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
後廠:
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的結果:
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)>'
太棒了,謝謝! – blundin