Reputation: 26
I am working through Hartl's Ruby on Rails tutorial and have stumbled across a problem that is related to a difference in the way the development and test environments are operating.
Simply put, the two environments are out of sync. The development environment appears to be working as normal, but some changes that are being made are not propagating to the test environment.
For example, making an edit in application.html.erb will result in that change being replicated in the test environment. However, making a change in _header.html.erb does not. This is how I noticed the problem - the tests for the existence of new links to the header were failing, whilst I was staring at them in the browser (in dev environment).
There is another quirk that I picked up on while trying to figure this out: if I run a local server on the test environment using rails server --environment test
, and boot up a browser, the application remains 'fixed'. What I mean by this is that any changes made to the application code, or HTML/CSS files, are not replicated in the browser. In order to see any changes, the local server has to be closed, and reopened. This then loads the test environment with the changes viewable.
The two environments db's are identical (I have run rake db:migrate
and rake db:test:prepare
as I add migrations throughout the tutorial, and have confirmed the db's are identical via the sqlite 3 viewer).
I imagine this problem stems from some rudimentary misunderstanding of Rails environments on my part - and any help would be greatly appreciated.
For completeness sake, I am working through chapter 8 (adding sign in/sign out authentication) when I first noticed this problem.
Upvotes: 0
Views: 241
Reputation: 4267
You are correct in noting that there are different behaviors for different environments. Only the development environment reloads the app on every browser request. The test and production environments cache basically everything in the app directory (see this SO answer) - basically, they only remember the version of code at the time the server started up. This is done for performance and/or to reduce volatility. Performance makes sense, but volatility may take some more explaining.
Say you're running your tests and editing code at the same time. The tests will run the version of code just before you ran the tests, and not any changes you made afterward. Reloading assets during testing would be like taking apart a car while it's on the highway, going at 60km/hr. Depending on what you're taking apart, it could have a whole lot of unintended consequences. Probably not catastrophic, but it would probably endanger running the tests, at the least.
If your problem is that your tests are failing because they're not running against the latest versions of code, then you may be experiencing caching issues - especially if you're using spork, which Hartl's Rails Tutorial recommended last I saw. You may need to simply restart spork.
Upvotes: 0
Reputation: 2946
Normally rails in production environment does not serve static files ( such as HTML and CSS ) if You want to do this You should say this in production.rb :
config.serve_static_assets = true
But if you run production, you supposed to run it with a webserver such as Apache or NginX and in this case they are responsible for serving static files.
Upvotes: 2