[英]Rspec-rails: Testing if layout includes styles or javascripts
I want to test if my 'style.css' is linked to my page while rendering it with a certain layout, for example 'application.html.erb': 我想测试我的'style.css'是否链接到我的页面,同时使用某个布局呈现它,例如'application.html.erb':
So here's the spec: 所以这是规范:
spec/views/layouts/application.html.erb_spec.rb 规格/视图/布局/ 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
And my application.html.erb looks like: 我的application.html.erb看起来像:
app/views/layouts/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>
When I run my view spec, it fails because 当我运行我的视图规范时,它失败了,因为
<link href="/stylesheets/style.css?1289599022" media="screen" rel="stylesheet" type="text/css">
is rendered, and 渲染,和
<link rel='stylesheet' type='text/css' media='screen' href='/stylesheets/reset.css'/>
is expected. 是期待。
And it's all about the numbers '?1289599022' rails adding after the filename. 这就是数字'?1289599022'在文件名后添加的栏目。 Any ideas on how to test this functionality?
有关如何测试此功能的任何想法?
Try using the Rails stylesheet_path() helper instead... this will generate the URL with the timestamp for you. 尝试使用Rails stylesheet_path()帮助器代替...这将生成带有时间戳的URL。
it 'renders a link to the stylesheet' do
render
rendered.should have_selector('link',
:rel => 'stylesheet',
:media => 'screen',
:href => stylesheet_path('style')
)
end
I've been running into the same problem today and it's been extremely frustrating. 我今天遇到了同样的问题而且非常令人沮丧。 I tried the solution above but did not have access to the stylesheet_path (I am using an rspec controller test but am rendering the view directly from the controller test).
我尝试了上面的解决方案,但没有访问stylesheet_path(我正在使用rspec控制器测试,但直接从控制器测试渲染视图)。 I tried including the necessary module to make it work but that just gave me other errors.
我尝试包含必要的模块以使其工作,但这只是给了我其他错误。 Eventually, I decided to just change rails in test mode such that it will not generate the asset timestamp.
最后,我决定只在测试模式下更改rails,这样它就不会生成资产时间戳。
Simply edit your test.rb to include the line: 只需编辑test.rb即可包含以下行:
ENV['RAILS_ASSET_ID'] = '' ENV ['RAILS_ASSET_ID'] =''
inside of 代替
Testapp::Application.configure do ... end Testapp :: Application.configure做...结束
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.