2012-12-12 40 views
1

我目前正在通過Michael Hartl的Ruby on Rails教程,並且在本教程的第3.2.2節中,他將談論如何使用RSpec來測試頁面的標題。我的測試(我自己寫的,跟着他的教程)一直沒有通過標題測試(它可以找到標籤,但標題總是一個空白字符串),並且Google上的一些搜索告訴我,我需要包含函數render_views以通過測試。我的問題是無論我嘗試什麼,RSpec都會爲render_views返回一個NoMethodError。這個事情我已經試過:render_views returned NoMethodError

  • config.render_viewsspec_helper.rb
  • describe "Static Pages" do render_views ... end在規範文件
  • describe "Static Pages" do RSpec::Rails::ViewRendering.render_views ... end在規範文件

他們全部返回NoMethodError

我不是跟隨他的Gemfile準確,因此Ruby版本和相關的寶石:

  • 紅寶石:1.9.3-P125
  • 導軌:3.2.9
  • rspec的: 2.12.0
  • RSpec的護欄:2.12.0
  • capycabra:2.0.1

我在工作g在RubyMine 4.5.4中,在Windows 7上運行。在命令提示符下運行測試會返回相同的錯誤。該規範文件是static_pages_spec.rb,位於spec/requests

的代碼在我static_pages_spec.rb

require_relative '../spec_helper' 

describe "Static Pages" do 
    #RSpec::Rails::ViewRendering.render_views, returns NoMethodError 
    #render_views, returns NameError 

    describe "Home Page" do 
    it "should have the h1 'Sample App'" do 
     visit '/static_pages/home' 
     page.should have_selector('h1', :text => "Sample App") 
    end 

    it "should have the title 'Home'" do 
     visit '/static_pages/home' 
     page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Home") 
    end 
    end 

    describe "Help Page" do 
    it "should have the h1 'Help'" do 
     visit '/static_pages/help' 
     page.should have_selector('h1', :text => "Help") 
    end 

    it "should have the title 'Help'" do 
     visit '/static_pages/help' 
     page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Help") 
    end 
    end 

    describe "About Page" do 
    it "should have the h1 'About Us'" do 
     visit '/static_pages/about' 
     page.should have_selector('h1', :text => "About Us") 
    end 

    it "should have the title 'About Us'" do 
     visit '/static_pages/about' 
     page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | About Us") 
    end 
    end 
end 

spec_helper.rb是通過創建創建與--no-test-framework項目運行後rails generate integration_test自動生成spec_helper.rb,在添加這些行所述config塊:

# Fix NoMethodError for method 'visit' 
config.include Capybara::DSL 

config.include Capybara::RSpecMatchers 

編輯 經過一些測試後,我設法通過將水豚版本更改爲1.1.2來通過測試,同時仍使用rspec 2.12.0和rspec-rails 2.12.0和而不是使用render_views。雖然我很高興測試通過了,但它仍然不能解決丟失render_views方法的問題,如果我嘗試使用它,它仍然會出現問題。

回答

3

那麼,經過多一點的測試後,解決了缺少render_views方法的奧祕。包括渲染視圖的方法,在spec_helper顯然config.include RSpec::Rails::ViewRendering在規範文件中spec_helperrender_views調用config.render_views之前需要。

其次,關於通過或失敗的測試,這個問題似乎是在水豚本身。顯然,在1.1.2和2.0.1中執行visit方法的方式看起來在返回方式上有很大不同,即使在包含render_views之後,測試仍然失敗(不使用config.include Capybara::RSpecMatchers)。使用它似乎提供了一個洞察其實施,雖然;使用RSpecMatchers來自Capybara的故障消息是它正在尋找CSS而不是HTML。然而,我並沒有聲稱完全理解它爲什麼會失敗,所以如果任何人都可以啓發我,爲什麼Capybara 2.0.1會導致測試失敗,而1.1.2允許測試通過,那就太好了。

0

水豚2.0要求所有測試都在規格/功能,而不是規格/要求。

您可以閱讀關於它的所有內容here

我建議你堅持哈特爾的教程中提到的版本,至少在你的初學者時代。將幫助您更快地完成教程!

+0

更改文件夾名稱實際上並沒有解決任何問題。我很確定這個問題與rspec有關,而不是實際上的水豚,因爲解決問題的方法來自rspec而不是水豚。 此外,它會是很好的知道解決這個問題不管,因爲一旦我開始構建新的應用程序,可能我反正遇到這樣的問題。 –

+0

您可以在教程中使用默認的替換render_views版本後嘗試:訪問'/ static_pages/home'?或者,您可以複製/粘貼原始問題中的完整spec_helper文件嗎? –