在Ruby on Rails Tutorial的Michael Hartl(惊人)final section之后,我收到以下错误:
1) User micropost associations status feed should include the microposts of followed users
Failure/Error: @user.feed.should include(mp3)
expected [#<Micropost id: 2, content: "Foo bar", user_id: 1, created_at: "2011-01-12 21:22:41", updated_at: "2011-01-12 22:22:41">, #<Micropost id: 1, content: "Foo bar", user_id: 1, created_at: "2011-01-11 22:22:41", updated_at: "2011-01-12 22:22:41">] to include #<Micropost id: 3, content: "Foo bar", user_id: 2, created_at: "2011-01-12 22:22:41", updated_at: "2011-01-12 22:22:41">
Diff:
@@ -1,2 +1,2 @@
-#<Micropost id: 3, content: "Foo bar", user_id: 2, created_at: "2011-01-12 22:22:41", updated_at: "2011-01-12 22:22:41">
+[#<Micropost id: 2, content: "Foo bar", user_id: 1, created_at: "2011-01-12 21:22:41", updated_at: "2011-01-12 22:22:41">, #<Micropost id: 1, content: "Foo bar", user_id: 1, created_at: "2011-01-11 22:22:41", updated_at: "2011-01-12 22:22:41">]
# ./spec/models/user_spec.rb:214
2) Micropost from_users_followed_by should include the followed user's microposts
Failure/Error: Micropost.from_users_followed_by(@user).should include(@other_post)
expected [#<Micropost id: 1, content: "foo", user_id: 1, created_at: "2011-01-12 22:22:46", updated_at: "2011-01-12 22:22:46">] to include #<Micropost id: 2, content: "bar", user_id: 2, created_at: "2011-01-12 22:22:46", updated_at: "2011-01-12 22:22:46">
Diff:
@@ -1,2 +1,2 @@
-#<Micropost id: 2, content: "bar", user_id: 2, created_at: "2011-01-12 22:22:46", updated_at: "2011-01-12 22:22:46">
+[#<Micropost id: 1, content: "foo", user_id: 1, created_at: "2011-01-12 22:22:46", updated_at: "2011-01-12 22:22:46">]
# ./spec/models/micropost_spec.rb:75
Finished in 9.18 seconds
153 examples, 2 failures
1) User micropost associations status feed should include the microposts of followed users
Failure/Error: @user.feed.should include(mp3)
expected [#<Micropost id: 2, content: "Foo bar", user_id: 1, created_at: "2011-01-12 21:22:41", updated_at: "2011-01-12 22:22:41">, #<Micropost id: 1, content: "Foo bar", user_id: 1, created_at: "2011-01-11 22:22:41", updated_at: "2011-01-12 22:22:41">] to include #<Micropost id: 3, content: "Foo bar", user_id: 2, created_at: "2011-01-12 22:22:41", updated_at: "2011-01-12 22:22:41">
Diff:
@@ -1,2 +1,2 @@
-#<Micropost id: 3, content: "Foo bar", user_id: 2, created_at: "2011-01-12 22:22:41", updated_at: "2011-01-12 22:22:41">
+[#<Micropost id: 2, content: "Foo bar", user_id: 1, created_at: "2011-01-12 21:22:41", updated_at: "2011-01-12 22:22:41">, #<Micropost id: 1, content: "Foo bar", user_id: 1, created_at: "2011-01-11 22:22:41", updated_at: "2011-01-12 22:22:41">]
# ./spec/models/user_spec.rb:214
2) Micropost from_users_followed_by should include the followed user's microposts
Failure/Error: Micropost.from_users_followed_by(@user).should include(@other_post)
expected [#<Micropost id: 1, content: "foo", user_id: 1, created_at: "2011-01-12 22:22:46", updated_at: "2011-01-12 22:22:46">] to include #<Micropost id: 2, content: "bar", user_id: 2, created_at: "2011-01-12 22:22:46", updated_at: "2011-01-12 22:22:46">
Diff:
@@ -1,2 +1,2 @@
-#<Micropost id: 2, content: "bar", user_id: 2, created_at: "2011-01-12 22:22:46", updated_at: "2011-01-12 22:22:46">
+[#<Micropost id: 1, content: "foo", user_id: 1, created_at: "2011-01-12 22:22:46", updated_at: "2011-01-12 22:22:46">]
# ./spec/models/micropost_spec.rb:75
Finished in 9.18 seconds
153 examples, 2 failures
似乎似乎未包含在Feed中。关于如何修复它的任何想法?或者在哪里查找代码中可能存在的错误?我将文件与Hartl's original code进行了比较;看起来确切。
感谢。
答案 0 :(得分:2)
是的,看起来您在设置关系时遇到问题。确保follow!
模型中的User
方法看起来与此完全相同
def follow!(followed)
relationships.create!(:followed_id => followed.id)
end