Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails asset pipeline tests pass production broken

I had the following problem with the asset pipeline.

  • I have an HTML email with an image inside.
  • I have tests covering the case in which the email is sent successfully.
  • All tests pass.
  • When going to production, the feature requiring the email to be sent is broken, because the HTML layout is referencing a non existent image.

This obviously applies to all precompiled assets.

It seems to me that suddenly tests are no longer reliable. Is there any way to avoid this situation to happen again?

like image 663
Fabrizio Regini Avatar asked Jun 12 '12 08:06

Fabrizio Regini


2 Answers

I found the perfect solution form my own case. If you set

 config.assets.compile = false 
 config.assets.digest = true

in test environment, your tests will rely on precompiled assets.

Since it is annoying to precompile assets every time during quick development and testing phases, in my case it's enough to have this configuration on CI only.

You can setup an initializer called ci_config.rb with the following:

if ENV['CI'] 
  class YourApp::Application
    config.assets.compile = false
    config.assets.digest = true 
  end
end

And configure you CI to run rake assets:precompile on start and rake assets:clean on end.

like image 125
Fabrizio Regini Avatar answered Oct 06 '22 20:10

Fabrizio Regini


Compare the default configuration options in application.rb, production.rb and development.rb, and read Configuring Rails Applications in Ruby on Rails Guide to learn options.

Important options are, followings:

config.serve_static_assets: set this to false (production default), then, rails will not serve static contents.

config.assets.compile: whether compile assets using asset pipeline to compile if needed.

If you set above two options to false (that's the default for production), then you need to 1) precompile and place static contents at proper places, 2) configure web server (apache or nginx, may be) to serve static contents as needed.

So, for production, you need not only place the files, but also configure web server to serve them OR you can configure serve_static_assets to create assets on-the-fly. You may need to adjust test configuration but for test serve_static_assets is true unless you change it.

like image 44
shigeya Avatar answered Oct 06 '22 21:10

shigeya