Rspec-rails: testing if the layout includes styles or javascripts

I want to check if my 'style.css' is attached to my page when rendering with a specific layout, for example 'application.html.erb':

So here is spec:

specs / 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 application looks like this:

application / 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 a spec of my kind, it fails because

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

and

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

.

And is it all about quantity? 1289599022 'rails adding after the file name. Any ideas on how to test this functionality?

+4
source share
2 answers

Try using the stylesheet_path () Rails helper instead ... this will create a timestamped URL 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 
+3
source

Today I faced the same problem, and it was very unpleasant. I tried the solution above, but did not have access to the stylesheet_path (I use the rspec controller test, but I give the view directly from the controller test). I tried to include the necessary module for it to work, but it just gave me other errors. In the end, I decided to just change the rails in test mode so that it does not generate a timestamp for the resource.

Just edit your test.rb to include the line:

ENV ['RAILS_ASSET_ID'] = ''

inside

Testapp :: Application.configure do ... end

0
source

All Articles