1

I have a CSS file that I want to run only in test mode, and not in development or production. I know how to do this inline as follows:

<% if Rails.env.test? &>
  <style>
    //My test only styles
  </style>
<% end %>

This works, however, I prefer to avoid inline stylesheets since they're unorganized and hard to maintain. I've tried applying it to an external stylesheet as such:

<%= stylesheet_link_tag 'my_stylesheet.css' if Rails.env.test? %>

But this only works if I add my CSS file to applications.css. And If I do this, the file gets added regardless of which environment I'm running in.

So my question is, how I can add an external stylesheet in rails that is only run while in test mode? For bonus points, how could I get rails to compile SASS that only runs in test mode?

1 Answer 1

1

This isn't a perfect solution, but you could consider adding a class to the html or body tag in test mode and then explicitly specifying that class for your test styles.

This would give you the clean separation and maintainability benefits but the trade-off is that you would still need these to be compiled by the asset pipeline.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.