gpt4 book ai didi

ruby-on-rails - rails 3 教程 : rspec + factory_girl_rails problem

转载 作者:行者123 更新时间:2023-12-02 00:36:10 24 4
gpt4 key购买 nike

我一直在关注 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 "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 没有任何元素。
请帮我解决这个问题,谢谢

最佳答案

嗯,答案很简单 :)我在微博规范中包含了微博关联。

很明显

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

关于ruby-on-rails - rails 3 教程 : rspec + factory_girl_rails problem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4526735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com