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.