2010-11-12 55 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做 ... 结束