2016-04-20 50 views
0

我試圖掌握TDD的一些概念,並在我的RoR應用程序中關於屬於static_pages#about的視圖。它具有route.rb get 'about' => 'static_pages#about'中定義的路由。一切工作到目前爲止瀏覽器,但我想也由RSpec測試它。 鑑於如何測試是否渲染了正確的模板(RSpec Rails)?

RSpec.describe "about.html.erb", type: :view do 
    it "renders about view" do 
    render :template => "about" 
    expect(response).to render_template('/about') 
    end 
end 

引發錯誤

Missing template /about with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :...... 

謝謝!

+0

您是否嘗試刪除'/'? 'expect(response).to render_template(「about」)' – fabersky

回答

2

這個規範沒有什麼意義 - 視圖規範的整體思想是渲染測試視圖,然後在其內容中編寫期望(TDD中的斷言)。查看規格有時可用於測試複雜的視圖,但在這種情況下不是您需要的。

如果您想要測試控制器是否呈現正確的模板,您可以在控制器規範中進行。

require 'rails_helper' 
RSpec.describe StaticPagesController, type: :controller do 
    describe "GET /about" do 
    it "renders the correct template" do 
     get :about 
     expect(response).to render_template "static_pages/about" 
    end 
    end 
end 

儘管這種規範的通常沒有什麼價值 - 你只是測試軌道的默認行爲,並通過feature spec它增加了更多的價值這可以涵蓋:

require 'rails_helper' 
RSpec.feature "About page" do 
    before do 
    visit root_path 
    end 

    scenario "as a vistior I should be able to visit the about page" do 
    click_link "About" 
    expect(page).to have_content "About AcmeCorp" 
    end 
end 

注意的是,這裏我們已經離開了TDD的世界,進入了所謂的行爲驅動開發(BDD)。這更關注軟件的行爲,更少關注軟件如何完成工作的細節。

+0

http://tutorials.jumpstartlab.com/topics/internal_testing/rspec_and_bdd.html – max