2010-11-12 38 views
4

我想測試我的'style.css'是否鏈接到我的頁面,同時呈現給定的佈局,例如'application.html.erb':Rspec-rails:測試佈局是否包含樣式或javascript

因此,這裏的規格:

規格/視圖/佈局/ application.html.erb_spec.rb

it 'should include styles for screen media' do 
    render 
    rendered.should have_selector('link', 
     :href => '/stylesheets/style.css', 
     :media => 'screen', 
     :rel => 'stylesheet', 
     :type => 'text/css' 
    ) 
    end 

而且我application.html.erb樣子:

應用程序/視圖/佈局/ application.html.erb

<html> 
    <head> 
    <meta content="text/html; charset=utf-8" http-equiv="content-type"> 
    <%= stylesheet_link_tag :style, :media => 'screen' %> 
    <%= javascript_include_tag :defaults %> 
    <%= csrf_meta_tag %> 
    </head> 
    <body> 
    <%= yield %> 
    </body> 
</html> 

當我跑我的看法規範,它失敗,因爲

<link href="/stylesheets/style.css?1289599022" media="screen" rel="stylesheet" type="text/css"> 

呈現,並

<link rel='stylesheet' type='text/css' media='screen' href='/stylesheets/reset.css'/> 

是期待。

這是關於在文件名後面添加的數字'1289599022'軌道。有關如何測試此功能的任何想法?

+0

鑑於你所展示的規格,我不認爲你的期望會是你寫的。 – zzawaideh 2010-11-12 22:12:52

+0

該規範根據rspec控制檯輸出真正尋找適當的鏈接標籤 – installero 2010-11-13 11:21:11

+0

它是水豚嗎?你是如何在視圖規範中工作的? http://groups.google.com/group/ruby-capybara/browse_thread/thread/6f8188ba35e170ff – whitered 2011-01-27 13:01:10

回答

3

嘗試使用Rails stylesheet_path() helper改爲...這會爲您生成帶有時間戳的URL。

it 'renders a link to the stylesheet' do 
    render 
    rendered.should have_selector('link', 
    :rel => 'stylesheet', 
    :media => 'screen', 
    :href => stylesheet_path('style') 
) 
end 
0

今天我一直在遇到同樣的問題,這是非常令人沮喪的。我嘗試了上面的解決方案,但沒有訪問stylesheet_path(我正在使用rspec控制器測試,但直接從控制器測試渲染視圖)。我試圖包括必要的模塊,使其工作,但只是給了我其他的錯誤。最終,我決定只是在測試模式下更換導軌,使其不會生成資產時間戳。

只需編輯您的test.rb包括行:

ENV [ 'RAILS_ASSET_ID'] = ''

Testapp :: Application.configure做 ... 結束

相關問題