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':
So here's the spec:
spec/views/layouts/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:
app/views/layouts/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. 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.
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). 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.
Simply edit your test.rb to include the line:
ENV['RAILS_ASSET_ID'] = ''
inside of
Testapp::Application.configure do ... end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.