Rails 3 教程:rspec +工厂女孩导轨问题

发布于 2024-10-08 22:17:16 字数 2828 浏览 3 评论 0原文

我一直在关注 Rails 教程(http://railstutorial.org/chapters/beginning,Rails 3 版本),并且在使用 Factory Girl 和 Rspec 时停在第 11 章,我有一个测试未通过我觉得我做错了什么,但我不明白是什么。
首先,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                 "[email protected]"
  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 没有任何元素。
请帮我解决这个问题,谢谢

i've been following the Rails tutorial (http://railstutorial.org/chapters/beginning , Rails 3 version), and i've stopped at 11th chapter when using Factory Girl and Rspec, I have a test that isn't passing and I feel I'm doing something wrong but I don't see what.
First of all there is a git repository on Github with the code that doesn't pass that test.
http://github.com/Monomachus/ch3_static_pages

So I got users model

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

  has_many :microposts
  .
  .
  .

I got microposts model

class Micropost < ActiveRecord::Base
  attr_accessible :content

  belongs_to :user

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

Then I got Factory girl settings

Factory.define :user do |user|
  user.name                  "Michael Hartl"
  user.email                 "[email protected]"
  user.password              "foobar"
  user.password_confirmation "foobar"
end

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

And finally Rspec code

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

And I got the error which definitely tells me that I do something wrong.

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

As you can see even the user_id property is not set correctly +
apparently @user.microposts doesn't have any elements.
Please help me with this issue thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

錯遇了你 2024-10-15 22:17:16

答案很简单:)
我在微帖子规范中包含了微帖子关联。

显然

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,然后一切就有意义了。因此,如果您确实遇到同样的问题,请将此代码包含到用户规范中。
现在我所有的测试都通过了:)

Well the answer was simple :)
I included microposts associations in the Micropost spec.

And clearly

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 did not contain the user properties but the micropost properties and of course @user = nil and then everything makes sense. So if you do have the same problem, include this code into User spec.
Now all my tests pass :)

深府石板幽径 2024-10-15 22:17:16

当我完成分页章节时,教程正在使用 Faker 创建 100 个示例用户(第 390 页上列出了 10.25 个),在 RubyMine 中我可以看到我的测试失败了,因为程序在重复的用户电子邮件上引发了异常地址(具有唯一约束)。 user_spec.rb 第 8 行的 @attr 有:email => “[email protected]",但是这会引发异常,因为它是重复的电子邮件(我猜是因为 Faker 已经创建了它)。

对我来说,修复方法是从第 8 行复制 @attr 并将其粘贴到描述“微帖子关联”块 (user_spec.rb) 中,然后将电子邮件地址更改为 :email => “[电子邮件受保护]”。我确信这完全是黑客行为,但我是个菜鸟。

更新:

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

By the time I had finished the pagination chapter, the tutorial was creating 100 sample users using Faker (listing 10.25 on page 390), and in RubyMine I was able to see my test was failing because the program was throwing an exception on duplicate user email address (which has a unique constraint). The @attr on line 8 of user_spec.rb has :email => "[email protected]", however this throws an exception since it's a duplicate email (I guess because Faker has already created it).

For me the fix was to copy @attr from line 8 and paste it into the describe "micropost associations" block (user_spec.rb), and change the email address to :email => "[email protected]". I'm sure this is a total hack but I'm a n00b.

Update:

Another fix for me was to comment out the line @user = User.create(@attr), and simply create @mp1 and @mp2.

煮茶煮酒煮时光 2024-10-15 22:17:16

尽管我在 user_spec.rb 中已经有了“微帖子关联”,但我在本节中也遇到了测试失败。结果我需要重新启动 spork 和自动测试,以便让它们使用factory.rb 中的新“micropost”工厂。

I was also getting test failure in this section, even though I already had "micropost associations" in user_spec.rb. Turns out I needed to restart spork and autotest in order to get them to use the new "micropost" factory in factories.rb.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文