rails 3教程:rspec + factory_girl_rails问题

时间:2010-12-24 14:04:24

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

我一直在关注Rails教程(http://railstutorial.org/chapters/beginning,Rails 3版本),我在第11章停止使用Factory Girl和Rspec,我有一个测试过去了,我觉得我做错了什么,但我看不出是什么 首先,在Github上有一个git存储库,代码没有通过该测试。 http://github.com/Monomachus/ch3_static_pages

所以我得到了用户模型

class User < ActiveRecord::Base
  attr_accessor :password
  attr_accessible :name, :email, :password, :password_confirmation

  has_many :microposts
  .
  .
  .


我有微博模型

class Micropost < ActiveRecord::Base
  attr_accessible :content

  belongs_to :user

  default_scope :order => 'microposts.created_at DESC'
end

然后我得到了工厂女孩设置

Factory.define :user do |user|
  user.name                  "Michael Hartl"
  user.email                 "mhartl@example.com"
  user.password              "foobar"
  user.password_confirmation "foobar"
end

Factory.define :micropost do |micropost|
  micropost.content "Foo bar"
  micropost.association :user
end

最后是Rspec代码

require 'spec_helper'

describe Micropost do
    .
    . 
  describe "microposts associations" do

    before(:each) do
      @user = User.create(@attr)
      @mp1 = Factory(:micropost, :user => @user, :created_at => 1.day.ago)
      @mp2 = Factory(:micropost, :user => @user, :created_at => 1.hour.ago)
    end

    it "should have a microposts attribute" do
      @user.should respond_to(:microposts)
    end

    it "should be in the reverse order of appearing" do
      @user.microposts.should == [@mp2, @mp1]
    end
  end
end

我得到的错误肯定告诉我,我做错了什么。

Failures:

  1) Micropost microposts associations should be in the reverse order of appearing
     Failure/Error: @user.microposts.should == [@mp2, @mp1]
     expected: [#<Micropost id: 2, content: "Foo bar", user_id: nil, created_at: "2010-12-24 12:47:02", update
d_at: "2010-12-24 13:47:02">, #<Micropost id: 1, content: "Foo bar", user_id: nil, created_at: "2010-12-23 13:
47:02", updated_at: "2010-12-24 13:47:02">],
          got: [] (using ==)
     Diff:
     @@ -1,3 +1,2 @@
     -[#<Micropost id: 2, content: "Foo bar", user_id: nil, created_at: "2010-12-24 12:47:02", updated_at: "20
10-12-24 13:47:02">,
     - #<Micropost id: 1, content: "Foo bar", user_id: nil, created_at: "2010-12-23 13:47:02", updated_at: "20
10-12-24 13:47:02">]
     +[]
     # ./spec/models/micropost_spec.rb:42:in `block (3 levels) in <top (required)>'

正如您所见,即使user_id属性设置不正确+
显然@ user.microposts没有任何元素 请帮助我解决此问题

3 个答案:

答案 0 :(得分:2)

答案很简单:) 我在Micropost规范中包含了微博协会。

显然

describe "microposts associations" do

  before(:each) do
    @user = User.create(@attr)
    @mp1 = Factory(:micropost, :user => @user, :created_at => 1.day.ago)
    @mp2 = Factory(:micropost, :user => @user, :created_at => 1.hour.ago)
  end

  it "should have a microposts attribute" do
    @user.should respond_to(:microposts)
  end

  it "should be in the reverse order of appearing" do
    @user.microposts.should == [@mp2, @mp1]
  end
end

@attr不包含用户属性,但是包含微博属性,当然@user = nil然后一切都有意义。因此,如果您遇到同样的问题,请将此代码包含在用户规范中。 现在我的所有测试都通过了:)

答案 1 :(得分:1)

当我完成分页章节时,本教程使用Faker创建了100个样本用户(在第390页上列出了10.25),在RubyMine中,我能够看到我的测试失败,因为程序抛出异常重复的用户电子邮件地址(具有唯一约束)。 user_spec.rb第8行的@attr有:email =&gt; “user@example.com”,但这引发了一个例外,因为它是一个重复的电子邮件(我想因为Faker已经创建了它)。

对我来说,解决方法是从第8行复制@attr并将其粘贴到描述“微博协会”块(user_spec.rb)中,并将电子邮件地址更改为:email =&gt; “user@example999.com”。我确定这是一个彻底的黑客,但我是一个n00b。

更新

对我来说另一个问题是注释掉@user = User.create(@attr)这一行,然后简单地创建@ mp1和@ mp2。

答案 2 :(得分:0)

我在本节中也遇到了测试失败,即使我已经在user_spec.rb中有“微博关联”。原来我需要重新启动spork和autotest才能让他们在factory.rb中使用新的“micropost”工厂。