← Back to Upcase

Testing a layout

(Edward Loveall) #1

My application’s layout has this piece of code:

<%= render 'shared/analytics' if Rails.env == 'production' %>

This partial contains a small script to track analytics. How would I test to make sure this gets rendered in production but not anywhere else?


I had a feeling that trying to set the environment in a test was a bad idea. So I did some digging and started to use a global constant hash (like APP_CONFIG[:show_analytics]) with any number of variables I could set. So now my layout has this line of code:

<%= render 'shared/analytics' if APP_CONFIG[:show_analytics] %>

Now in a test I can just set APP_CONFIG[:show_analytics] = true and then false for clean up. However, where do I test this? Is there such thing as a layout spec, or is it a special view spec? Am I even using the right pattern?

Sorry for the double question.

0 Likes

(Luís Ferreira) #2

There are view specs in RSpec: https://www.relishapp.com/rspec/rspec-rails/v/2-0/docs/view-specs/view-spec

0 Likes

(Edward Loveall) #3

Thanks @zamith! Just for good style, would I call the file _application.html.erb_spec.rb? Also i’m used to describing layouts as contacts/show or something like that. Unless I find otherwise, it seems like i should just describe the file like describe layouts/application.html.erb do.

Any thoughts?

0 Likes

(Luís Ferreira) #4

I would probably have a file in spec/views/layouts/application.html.erb_spec.rb, yes.

0 Likes