问题
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?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/10992893/rails-asset-pipeline-tests-pass-production-broken