I want to run unit tests in a Cron Job in a Production environment. Due to restrictions in the # of machines I have in my disposal, I can only run these units tests in 1 production machine. I don't have a development machine/environment. Let's imagine this is the case (even if it's not a realistic and a bad setup) and I can't change that at all :)
Is it possible to run "rake test" or even "rake test RAILS_ENV=test" in a production environment? And if I do, would it always run the tests on the test database, not on the production one? And it it does, would the environment be "test" only within the scope of the test?
Obviously, I could test this, but because I don't want to risk production data, I rather have a firm answer before doing this.
Yes, this will work just fine. Make sure you specify the RAILS_ENV environment variable like you already have and that you have the test database configured in your database.yml file.
Related
I am teaching myself Ruby On Rails with Michael Hartl's Book. When it uncovered the use of seeds.rb file, I tested within Development Environment, it Failed. When set to Test Environment, It Succeeded. Why? When will I need to change the environment again for successful tests?
When you say I tested within Development Environment, it Failed., you are not running automated tests. You ran the rake db:seed script against the development database. The same task can be run against the test environment with rake db:seed RAILS_ENV=test. Again, this is not an automated test.
There are many reasons why rake db:seed run against the development environment failed in your case. The specific reason could be identified based on the error message.
development environment is one where you work on a day to day basis, adding/changing functionality by making code changes. By default, most scripts assume that you are working with development environment.
test environment is the environment against which the automated tests are run. In the case of rails tutorial, the automated tests are written in the files under test folder. When automated tests are run on a rails application - with rake test or some other way - the test environment is used to run these tests against. The test database gets cleaned up before running the tests to ensure the tests are run starting with a blank state.
Hope this clarifies.
I've been struggling with a problem for a few days right now and I want to know how I can force my Rails app to run in environment and then I want to be able to test the images to ensure that they are http or https.
I'm getting closer and closer to solving the problem but now I want to test my production site instead of my test. The reason being is that when I run my rake tests to check to see if the images have either http or https, it only gives me a relative link such as
/images/9995/0007/company_logo.png
This is not helpful to me at all. In order to assert_match /http:/ patten, I need it to grab the images on the production site not in test. How can I do this? I've been researching this for a very long while now and I still feel that I'm no where close to figuring this out.
I've tried to force the Rails environment into production by placing this line of code into config/environments.rb
ENV["RAILS ENV"] ||= 'production'
THe problem is, when I run my tests, doesn't it still refer to the test database? How do I know that when I run rake test TEST=test/functional/ect_test.rb its running production?
I've also tried to have my database.yml file to "point" to production but I made a huge mess of this and to quite frankly I don't really know how that works (It was a recommendation from another stack overflow site). Anyways, some help would be greatly appreciated.
*FYI I am running on Rails 2.3. We are in the middle of upgrading our database. Any input would be greatly appreciated. Thanks.
It's just a matter of starting your server or running your tests with the environment set to production. To do this just prepend RAILS_ENV=production to the commands rails server or rake test or whatever you run you server or tests with. The line:
ENV["RAILS ENV"] ||= 'production'
Is misspelled (just mentioning in case). There's an underscore missing in it. Also, it won't set the RAILS_ENV to production if that ENV value is already set. Make it:
ENV["RAILS_ENV"] = 'production'
To ensure you overwrite any value. Although the aforementioned command prepend technique should work.
Also, if your site runs in https you can be sure the images with relative paths will be served via https as well.
In my job we have a development, test and production environments. Development is the local environment use for development, test is a shared environment use for general purpose testing (QA) and production is well for production.
We are starting to development is Rails, in this framework the 'test' environment is use for running the specs. How do we specify the new environment to run the unit tests?
I have try to change the value of RAILS_ENV on "test_helper" but when running "rake test" it still try to connect to the test database.
I have an Rails app, with some initializer code which must be executed when the app is running in development mode. However, this initializer code must not be executed when running tests.
I have established that
$ rake test
causes the app to be run in development mode, which invokes the initializer code and therefore breaks my tests. This is expected behaviour apparently (see: https://github.com/rails/rails/issues/9801).
What is the correct command to run my Rails app tests without starting the app in development mode?
Does your test_helper.rb file look like the default? It should start with:
ENV["RAILS_ENV"] = "test"
Try running the rake task with an explicit environment:
rake test:units RAILS_ENV=test
If you don't specify an environment, development is assumed, in my experience. And while the test database still gets the fixture data inserted into it, stuff from the development environment still gets referenced for some reason.
You need to have a line at the start of the test_helper.rb file that says
Rails.env = "test"
You can't use
ENV["RAILS_ENV"] = "test"
because it will fail to clear the cached value that returns from calls to Rails.env.
I'm writting a rails application by TDD, So, I was wondering, how do I test production app, like having a different security token from development security token, how to ensure that all the configurations are correct in production, like mail configs etc.
Shouldn't we never run tests in production mode, since it would wipe the database.
So, how should one go about testing a rails app in production
The method many developers use is creating another environment called stage. You can make your configuration identical to production, and deploy to this before production to ensure everything works correctly. You can run your integration tests / performance tests against staging if you like.
You can copy the production config (config/environments/production.rb) to a new file called stage.rb, then remember to populate your database.yml with the new connection details for a stage database.
When you run tests you are always in the test environment since RAILS_ENV is hardcoded in test_helper.rb. Setting RAILS_ENV=production means the schema will be cloned from the production database rather than the development database. I could of course set up a development database on the production servers, but that doesn't seem to make sense.
Given all the differences there are between my development and production server - operating system, web server, database, gems etc. - I can't really feel comfortable deploying my application unless I have first run my test suite not only in development but also in production. Thanks to the beautiful and powerful API of Capistrano this is a walk in the park to accomplish:
Here goes the example how it seems to be done -
desc "Run the full tests on the deployed app."
task :run_tests do
run "cd #{release_path} && RAILS_ENV=production rake && cat /dev/null > log/test.log"
end
desc "Copy in server specific configuration files"
task :copy_shared do
run <<-CMD
cp #{shared_path}/system/voxway.rb #{release_path}/config &&
cp #{shared_path}/system/database.yml #{release_path}/config
CMD
end
desc "Run pre-symlink tasks"
task :before_symlink, :roles => :web do
copy_shared
run_tests
end