2012-07-11 48 views
1

我一直在關注Michael Hartl的Rails教程。實際上我已經完成了它,但是我在最後一章的最後一個練習中做了一些重構,出現了一些問題。我所做的只是改變放映視圖從該用戶模型(show.html.erb):當應用程序按預期工作時,爲什麼這些RSpec測試失敗?

<section> 
    <h1> 
    <%= gravatar_for @user %> 
    <%= @user.name %> 
    </h1> 
</section> 

要這樣:

<section>  
    <%= render 'shared/user_info' %> 
</section> 

而在部分(_user_info.html.erb)我有這樣的:

<a href="<%= user_path(current_user) %>"> 
    <%= gravatar_for current_user, size: 52 %> 
</a> 
<h1> <%= current_user.name %> </h1> 
<% unless current_page?(user_path(current_user)) %> 
    <span> <%= link_to "view my profile", current_user %> </span> 
    <span> <%= pluralize(current_user.microposts.count, "micropost") %> </span> 
<% end %> 

,一切工作正常的瀏覽器,但由於某種原因rspec的失敗一些測試,我懷疑調用它在sessions_helper.rb定義的CURRENT_USER方法,rspec的是有問題,這對w- ay包含在application_helper.rb中。 gravatar_for函數在users_helper.rb中定義。下面是我從測試中得到的錯誤:

故障/錯誤:前{訪問user_path(用戶)} ::的ActionView ::模板錯誤: 未定義的方法email' for nil:NilClass # ./app/helpers/users_helper.rb:4:in gravatar_for」 #./app/views/shared在 #./spec/requests/user_pages_spec.rb:58:in`塊(3級)'/_user_info.html.erb:1:in _app_views_shared__user_info_html_erb___3480157814439046731_47327400' # ./app/views/users/show.html.erb:5:in _app_views_users_show_html_erb___1252254778347368838_47378900'

我將不勝感激,如果你能不能幫我鑑定一下正在這裏。我可以找到不同的方式來做同樣的事情,但我對此很好奇。我對Rails的經驗並不是很豐富,這就是爲什麼我遵循教程,所以如果我錯過了某些明顯的東西,請原諒我。謝謝。

+0

您是否試圖在顯示頁面上顯示登錄用戶的信息或您的頁面上的特定用戶? – 2012-07-11 11:32:00

+0

其實,我使用相同的部分爲主頁和登錄用戶的個人資料頁面,因爲該特定的內容如果非常相似(除非在非除非的內容)。該應用程序託管在heroku上,這裏是鏈接:[link](https://afternoon-river-7478.herokuapp.com/),您可以通過電子郵件登錄:[email protected],傳遞:foobar – Jose 2012-07-11 11:51:02

回答

3

我想我得到了解決方案。雖然我仍然困惑。這是因爲他們在我的問題中提到的重構前的測試代碼(因爲他們在本教程中,他們都是通過):

describe "profile page" do 
    let(:user) { FactoryGirl.create(:user) } 
    let!(:m1) { FactoryGirl.create(:micropost, user: user, content: "Foo") } 
    let!(:m2) { FactoryGirl.create(:micropost, user: user, content: "Bar") } 

    before { visit user_path(user) } 

    it { should have_selector('h1', text: user.name) } 
    it { should have_selector('title', text: user.name) } 

    describe "microposts" do 
    it { should have_content(m1.content) } 
    it { should have_content(m2.content) } 
    it { should have_content(user.microposts.count) } 
    end #some other tests for show page continue, also having the same behaviour 
end 

在閱讀測試,看看是否有一個錯誤,我開始想,爲什麼這些試驗是路過,如果我不籤的用戶,所以我添加的代碼在用戶登錄前擋,像這樣:

describe "profile page" do 
    let(:user) { FactoryGirl.create(:user) } 
    let!(:m1) { FactoryGirl.create(:micropost, user: user, content: "Foo") } 
    let!(:m2) { FactoryGirl.create(:micropost, user: user, content: "Bar") } 

    before do 
    valid_signin user 
    visit user_path(user) 
    end 

    it { should have_selector('h1', text: user.name) } 
    it { should have_selector('title', text: user.name) } 

    describe "microposts" do 
    it { should have_content(m1.content) } 
    it { should have_content(m2.content) } 
    it { should have_content(user.microposts.count) } 
    end #some other tests for show page continue, also having the same behaviour 
end 

而現在通過了所有測試。我知道登錄用戶似乎很愚蠢而且顯而易見,但是這就是測試如何在the tutorial之前進行的。如果你想檢查它,Here是更新的測試文件。所有更改現在在我的github回購中提交。感謝大家的幫助。

+0

好的,剩下要做的就是將您的答案標記爲每個人參考的可接受解決方案。 – 2012-07-12 10:22:09

1

聽起來就像您在測試數據庫中沒有任何用戶。開發數據庫和測試數據庫是兩個不同的東西。 Rspec在運行測試時使用測試數據庫。

要設置測試數據庫,請執行rake db:test:prepare

然後,您需要在運行規格時填充測試數據庫。一個很好的方法是使用Factory Girl寶石。

+0

current_user實際上是一個在sessions_helper.rb中定義的方法,但我不明白爲什麼rspec不會調用該方法。我嘗試了你的建議,但得到了同樣的錯誤。 – Jose 2012-07-11 11:57:25

+0

請看更新的回答 – Dty 2012-07-11 12:07:10

+0

這也行不通。儘管我已經按照您的建議再次準備好了測試數據庫,只是爲了確保。我不認爲這是問題,作爲教程的一部分,我使用Factory Girl並不斷準備測試數據庫。其實,即使我直接從視圖(而不是部分)調用current_user函數,它也不起作用,所以我想這不是一個問題與部分,但與輔助方法,只是無法弄清楚然而。 – Jose 2012-07-11 12:24:44

2

閒逛your repo,這可以是問題:有一個在Rails的教程報道,6月25日與在應用程序/傭工/ sessions_helper的sign_insign_out方法問候的current_user設置一個小錯誤.rb文件。該通知似乎並沒有被貼在Rails Tutorial News site,但它發送給訂戶,它體現在current online book說改變:

self.current_user = user # in the sign_in method 
self.current_user = nil # in the sign_out method 

因此,嘗試更新your sessions_helper.rb,並看看是否能阻止你從獲得nil用戶。

+0

有趣,感謝您的更新。我想在更新之前我就通過了這個部分。但是,它沒有固定測試(但我會保留更改)。我相信,固定測試的是我的一個愚蠢的錯誤。檢查我的答案的細節。感謝您的幫助。 – Jose 2012-07-12 03:29:10