Ruby on Rails: Running Tests - ruby-on-rails

When I want to run all my unit tests, I run rake test:units. To run all my functional tests, I run rake test:functionals. If I want to run all the test cases in one file, I run
ruby test/unit/username_test.rb
A few people have been telling me I should run rake instead such as
rake test:units TEST=test/unit/username_test.rb
For running tests, they say I should always run rake. I know I should run rake if I'm testing all my unit tests. But what if it's just one file or one particular test method in a file that I'm testing? Should I still use rake? Is there any difference between the two? Do I get any benefit from running rake over ruby? Is there any disadvantage to running ruby rather than rake?

Sadly, the two are not the same. Running the tests under rake can wind up pulling things from different places than when you run the test directly (more a problem when you have multiple versions of gems, etc. on your system).
The intent is that tests run under rake should be in an environment that matches what rails would produce; I can not attest to how closely they match, but I have seen tests that passed when run directly but failed when run via rake or rails (and visa versa).

Before checking in at the very least I'd recommend running rake to hit everything, in order to be assured that nothing unexpected has broken.
Plain ruby seems ideal for fast testing of single files during iterations.
Be aware that running everything through rake can produce different results to running everything individually, as I found to my confusion recently - I was doing something slightly wrong in one test that worked successfully in isolation but that left a problem lying around for a subsequent test that only showed up when I used rake.

No I dont think so. Rake seems to be a convenient way to run all tests, all unit tests or all functional/controller tests.
For a single file, I use the ruby object_test.rb approach.. shorter and works fine for my rails home project.

They should be identical. if they are not, you're definitely doing something wrong.
As I said in my other comments, if you get tests that pass in one, but fail in the other, you're doing something very wrong; this indicates a poor test setup, and is usually caused by a different test run order between the two test approaches; one of which causes tests to fail.
Usually the cause of this is that you are not using transactions and/or tests are not cleaning up after themselves. For example, not properly requiring the fixtures they later test for, and instead relying on the pre-existing database state.
you are free to use either method. if something breaks, you're doing something wrong in your tests, and you should fix your code.

The two are not the same. Rake will do some preliminary test loading.
The intent is that tests run under rake should be in an environment that matches what rails would produce;
One difference I've noticed is with rake some of the fixture loading happens which could be by-passed with ruby.
I'd recommend using rake, unless you are using the ruby command line to one just one test in the file with the -n option.

Related

Inconsistent Rails Test Results

The first couple times I run rspec spec, I receive failures and if I run it again and thereafter, it passes. Why is this happening and how do I fix it?
This appears to be related to uniqueness code to prevent adding a record with a name that has already been. Below is the test that consistently fails the first time:
it { should validate_uniqueness_of(:name).case_insensitive.with_message(/has already been taken. Please use a different name./) }
The full repo can be found here: https://github.com/melissajstudent/koth
You are right in thinking that the uniquess is a good indicator of what’s happening. Likely your test database has some remaining records from the last time you ran the tests and is trying to creat records with the same factories.
To that end, there are a few ways to clean your database in between runs of your test suites. Sometimes it’s helpful just to run bundle exec rake db:test:prepare Before running your test suite. In other cases you can implement some more robust database cleaning throughout your run with the database cleaner gem: https://github.com/DatabaseCleaner/database_cleaner

Why "rake test" and "rake test:functionals" give different results?

I've had a weird event of having rake test and rake test:functionals producing different results on a Rails 3 app that uses fixtures to insert test database records. The first group was failing on one of the controller tests, while the other one was successfully passing.
I also tried to run the failing test with ruby -I test path/to/file and zeus test path/to/file, but those runs were successful. The issue was reproduced on different machines. The test was failing because of the state of the test database, which seemed impossible to me when I looked at the fixtures.
Strangely enough, the issue disappeared the next day and I no longer can reproduce it. What could be the cause of such a problem and how to avoid it?

Running a single rails performance test

I am trying to run a single benchmark in the test/performance/ directory. kinda like rake test:benchmark would do, but only with a single test.
The reason for this is because the whole performance suite takes a fair amount of time to run, and I am only interested in doing tests the model that the change will effect at first.
I have tried this, but it does not setup the benchmarking environment:
ruby -Ilib:test test/performance/email_list_test.rb
Also tried rails benchmarker EmailList.all, but I believe that must be pulling from the standard unit tests.
Try this:
rake test:benchmark TEST=test/performance/email_list_test.rb

Rails 3 Rake Clone Database for Testing Environment

Is there a rake command in Rails 3 to clone my development database data? I noticed rake db:test:prepare and rake db:test:clone are mentioned throughout various blogs, but running them seems to do nothing. Furthermore, rake -T shows no db:test cases. I've resorted to loading a sql dump for now, but it would be great if I could just clone my existing development data for up-to-date testing.
EDIT --
I desire to test on a database since I am dealing with legacy data that I run through model filters when accessed. Factories won't work for me in this context, since data passed through create is defined as a different schema than that of the legacy data.
rake db:test:prepare is still there even though it doesn't show up in rake -Tdb. I guess the Rails team decided to de-clutter the rake -T output?
I would suggest you not clone your development database but rather rely on factories to give you predictable data you can craft for your exact test cases. Sooner or later, relying on having reliable test data in a database you can access will break your tests. It will also break the tests of anyone else who works on the project. And changes/additions to the data will not propagate to other developers as would your carefully constructed factories.
Look over Machinist, FixJour, FactoryGirl and the lot. They really solve the test data problem well and you check them into version control so the rest of your team has access to them.

Why is Rake running a model for which I can find no test?

When I run "rake", it's loading up one of the models among all of the classes I have in my app/models directory. The thing is, it's not one I have a test for, it's just a model I have in there that is actually used with script/runner to run in the background and perform tasks for my main Rails application. At the end of the file I've got it creating a new instance of the class above and then running main for the class.
Since it loops indefinitely I definitely do not want it started up by the testing code. Why would the unit testing or Rake involve this other class in any way?
To my shame, I haven't been writing any tests for this code and I decided I would start writing some, but this stopped me right away because I can't even run Rake for what is out there now without it going haywire.
I'm not sure it's Rake's fault - I have a feeling that when you add :environment as a dependency, you're bringing up the whole Rails infrastructure, which may well involve requiring every model file (this is fairly wild guesswork - I haven't followed the boot process that deeply yet).
However it's happening, it seems that your model is being required, at which point all hell breaks loose.
Looking at script/runner and more usefully, railties/lb/commands/runner.rb, the execution sequence seems to be something like:
require 'boot' # boot the Rails app
eval(File.read(code_or_file)) # run what you asked for
That second line (it's actually around line 45 in runner.rb) looks like the key. How would it be if you defined a separate script (in /lib, say?) that contained the code that runs your model? I think that would probably be a more Rails-ish way to do it. And it would probably stop Rake messing up your tests...

Resources