我很惭愧提出这个问题,但是我找不到我的错误,我再一次从Michael Hartl那里做导轨教程,因为我第一次无法做正确的TDD,无论如何我有设法完成每一项测试,但只有一次!问题是我在rails'控制台上手动测试它并且它工作了,我尝试用rake db:reset
和rake test:prepare
重置数据库,显然重新填充数据库rake db:populate
确定,直接到关键......我有以下测试
describe "status" do
let(:unfollowed_posts) do
FactoryGirl.create(:micropost, user: FactoryGirl.create(:user))
end
let(:followed_user) { FactoryGirl.create(:user) }
before do
@user.follow!(followed_user)
3.times { followed_user.microposts.create!(content: "Lorem Ipsum") }
end
its (:feed) { should include(newer_micropost) }
its (:feed) { should include(older_micropost) }
its (:feed) { should_not include(unfollowed_posts) }
its (:feed) do
followed_user.microposts.each do |micropost|
should include(micropost)
end
end
end
根据我的说法,一切正常,我在rails console
上手动测试,但是当使用rpsec spec/models/user_spec.rb
运行规范时,会出现以下错误
Failures:
1) User micropost associations status feed
Failure/Error: should include(micropost)
expected #<ActiveRecord::Relation [#<Micropost id: 355, content: "Lorem ipsum", user_id: 702, created_at: "2014-05-10 03:34:47", updated_at: "2014-05-10 04:34:47">, #<Micropost id: 354, content: "Lorem ipsum", user_id: 702, created_at: "2014-05-09 04:34:47", updated_at: "2014-05-10 04:34:47">]> to include #<Micropost id: 358, content: "Lorem ipsum", user_id: *703*, created_at: "2014-05-10 04:34:47", updated_at: "2014-05-10 04:34:47">
Diff:
@@ -1,2 +1,3 @@
-[#<Micropost id: 358, content: "Lorem ipsum", user_id: *703*, created_at: "2014-05-10 04:34:47", updated_at: "2014-05-10 04:34:47">]
+[#<Micropost id: 355, content: "Lorem ipsum", user_id: 702, created_at: "2014-05-10 03:34:47", updated_at: "2014-05-10 04:34:47">,
+ #<Micropost id: 354, content: "Lorem ipsum", user_id: 702, created_at: "2014-05-09 04:34:47", updated_at: "2014-05-10 04:34:47">]
</code>
Failure/Error: should include(micropost)
expected #<ActiveRecord::Relation [#<Micropost id: 355, content: "Lorem ipsum", user_id: 702, created_at: "2014-05-10 03:34:47", updated_at: "2014-05-10 04:34:47">, #<Micropost id: 354, content: "Lorem ipsum", user_id: 702, created_at: "2014-05-09 04:34:47", updated_at: "2014-05-10 04:34:47">]> to include #<Micropost id: 358, content: "Lorem ipsum", user_id: *703*, created_at: "2014-05-10 04:34:47", updated_at: "2014-05-10 04:34:47">
Diff:
@@ -1,2 +1,3 @@
-[#<Micropost id: 358, content: "Lorem ipsum", user_id: *703*, created_at: "2014-05-10 04:34:47", updated_at: "2014-05-10 04:34:47">]
+[#<Micropost id: 355, content: "Lorem ipsum", user_id: 702, created_at: "2014-05-10 03:34:47", updated_at: "2014-05-10 04:34:47">,
+ #<Micropost id: 354, content: "Lorem ipsum", user_id: 702, created_at: "2014-05-09 04:34:47", updated_at: "2014-05-10 04:34:47">]
</code>
根本没有碰到follow_user,所以...现在我很头疼,为什么会这样?这是一个关于rspec的虚假行为,提前感谢,并为这个沉闷的问题而烦恼。
答案 0 :(得分:0)
您需要更改feed
文件
user.rb
方法
这
def feed
Micropost.where("user_id = ?", user)
end
到
def feed
Micropost.from_users_followed_by(self)
end
以及将方法self.from_users_followed_by
添加到micropost.rb
文件
def self.from_users_followed_by(user)
followed_user_ids = "SELECT followed_id FROM relationships WHERE follower_id = :user_id"
where("user_id IN (#{followed_user_ids}) or user_id = :user_id", user_id: user.id)
end