我很慚愧地提出這個問題,但我找不到我的錯誤,我再次從邁克爾哈特爾那裏學習rails教程,因爲第一次我無法做到正確TDD,無論如何,我已經成功地進行了每一個測試,但只有一個!事情是,我在軌道上的控制檯手動測試它,它的工作原理,我試圖重置數據庫與rake db:reset
和rake test:prepare
和明顯重新填充數據庫rake db:populate
好吧,直接點...我有以下測試RailsTutorial rspec測試失敗
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>
I don't understand why the user_id is changed if the code 3.times { followed_user.microposts.create!(content: "Lorem Ipsum") }
不觸及followed_user都來了,所以......這是我的現在頭痛,爲什麼會發生這種情況?這是rspec的一個虛假行爲,預先感謝,併爲這個沉悶的問題提供。
你正在使用哪個版本的Rails,以及你在教程中的位置是什麼? – pawel7318
我目前使用rails 4.0.4和ruby 2.0.0,使用postgresql作爲數據庫,這是最後一章[http://www.railstutorial.org/book/following_users#sec-motivation_and_strategy]代碼示例是11.41中列出的那個 – Coronellx