FactoryGirl没有创建独特的对象

时间:2013-06-28 16:38:03

标签: ruby ruby-on-rails-3 rspec2 factory-bot faker

我有一个失败的测试,根据我的理解,它应该使用Faker gem创建具有唯一值的对象。我真的被困在这里了。谢谢你的帮助!

来自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)>'

1 个答案:

答案 0 :(得分:1)

在每个:let!块中,评估的最后一行是add_tags!方法,它似乎返回true而不是添加标记的帖子。因此,在您的规范中[newer_post, older_post]实际上等于[true, true]而不是您的实际帖子。

在每个:let!块的末尾,返回帖子:

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

或者,您可以更改add_tags!方法以返回帖子,而不是true / false