Railsチュートリアル(http://railstutorial.org/chapters/beginning、Rails 3バージョン)に従っていて、Factory GirlとRspecを使用しているときに第11章で停止しましたが、合格していないテストがあります何か間違ったことをしているように感じますが、何がわかりません。
まず第一に、そのテストに合格しないコードを含むgitリポジトリがGithubにあります。
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には要素がありません。
この問題のおかげで私を助けてください。