2016-06-27 45 views
0

目前,這是一個簡單的項目 - 只是幾個靜態頁面。我正在開發一個通用的測試框架,但我正努力區分不同的測試選項。我已經添加了Rspec,水豚,Faker,工廠女孩,春季和應該(儘管我目前沒有使用應用程序匹配器)。capybara have_title NoMethodError

我有這個控制器測試文件:

require 'rails_helper' 

RSpec.describe StaticPagesController, type: :controller do 

    describe "GET #a_page" do 
    before(:each) { get :a_page } 

    it "returns http success" do 
     expect(response).to have_http_status(:success) 
    end 
    it "has a page title Static Site" do 
     expect(response).to have_title('Static Site') 
    end 
    end 

end 

當這個貫穿後衛,它拋出一個錯誤堆棧:

23:13:39 - INFO - Run all 
23:13:39 - INFO - Running all specs 
Running via Spring preloader in process 4498 
Running via Spring preloader in process 4506 
/home/steve/workspaces/static_site/db/schema.rb doesn't exist yet. Run `rake db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /home/steve/workspaces/static_site/config/application.rb to limit the frameworks that will be loaded. 
.F 

Failures: 

    1) StaticPagesController GET #a_page has a page title Static Site 
    Failure/Error: expect(response).to have_title('Static Site') 

    NoMethodError: 
     undefined method `match' for nil:NilClass 
     Did you mean? catch 
    # /home/steve/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/queries/title_query.rb:18:in `resolves_for?' 
    # /home/steve/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/node/document_matchers.rb:20:in `block in assert_title' 
    # /home/steve/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/node/simple.rb:144:in `synchronize' 
    # /home/steve/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/node/document_matchers.rb:19:in `assert_title' 
    # /home/steve/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/rspec/matchers.rb:105:in `matches?' 
    # ./spec/controllers/static_pages_controller_spec.rb:34:in `block (3 levels) in <top (required)>' 
    # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call' 
    # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-1.7.1/lib/spring/command_wrapper.rb:38:in `call' 
    # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-1.7.1/lib/spring/application.rb:191:in `block in serve' 
    # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-1.7.1/lib/spring/application.rb:161:in `fork' 
    # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-1.7.1/lib/spring/application.rb:161:in `serve' 
    # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-1.7.1/lib/spring/application.rb:131:in `block in run' 
    # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-1.7.1/lib/spring/application.rb:125:in `loop' 
    # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-1.7.1/lib/spring/application.rb:125:in `run' 
    # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-1.7.1/lib/spring/application/boot.rb:19:in `<top (required)>' 
    # -e:1:in `<main>' 

Finished in 0.029 seconds (files took 2.54 seconds to load) 
2 examples, 1 failure 

Failed examples: 

rspec ./spec/controllers/static_pages_controller_spec.rb:33 # StaticPagesController GET #a_page has a page title Static Site 

的第一個測試運行正常,並沒有第二個,我得到一個乾淨的結果。我花了很多時間去檢查我的配置,它看起來不錯。我還查看了文檔和一些支持網站。

任何人都可以幫忙嗎?

回答

1

水豚匹配器需要針對html/xml文檔元素(或解析爲文檔的字符串)而不是針對響應對象進行調用。默認情況下,Capybaras匹配器通常只在特性和視圖規格(不是控制器)中可用,您是否有將其納入控制器規格的特殊原因?驗證頁面標題真的應該更傾向於作爲視圖規範而不是控制器(默認情況下,視圖不在控制器規範中呈現 - https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs)。

+0

感謝您的反饋 - 這是有道理的。在閱讀了很多內容之後,我想我只傾向於爲模型,控制器(不包括我在OQ中建議的測試類型)和功能創建測試。查看測試似乎沒有帶來什麼,所以我不認爲我會打擾他們。這聽起來是對的嗎? –

+0

@SteveRoach編寫任何測試可以幫助您提高工作效率 - 視圖測試對功能測試的一個優勢是它們往往速度更快,但當您的頁面變得更受JS驅動時,它們開始失去實用性,因爲它們只能真正檢查靜態內容。我個人的看法是TDD的事情讓很多人過度測試了一些東西。 –

+0

謝謝你的湯姆。我認爲我將更多地依靠View來進行功能測試,主要是針對每件事都只有一種方法。我希望能夠很好地處理一件事。也許在路上我會混淆一下。我現在要解決速度問題。我試圖讓測試平衡正確,我想這也會帶來更多的經驗。與此同時,我正試圖建立一個標準(對我來說)測試的庫,這將爲我的任何項目奠定良好的基礎。然後在此基礎上構建。感謝您的反饋 - 這一切都很好。 –