Autotest

Posted by Janos on January 11, 2010

Autotest is a convenient way to keep your tests passing without having to continuously issue the rake rspec commands.
To use autotest, create a Rails project if you have not done so already:

rails huhu

cd into the new directory and enable RSpec:

./script/generate rspec

Now generate two RSpec-enabled models:

./script/generate rspec_model book title:string
./script/generate rspec_model author full_name:string

Notice how now you have two tests in spec/models.
Before running the tests, make sure your test database is ready:

rake db:migrate RAILS_ENV=test

Now from the root directory of the Rails application, execute:

./script/autospec

You should see all tests passing. Observe that the script does not exit.
Open a second shell and add a test like the following to spec/models/author_spec.rb:

it "should be stuff" do
    false.should== true
  end

Obviously, you would write a meaningful test but it illustrates the point. As soon as you save the file, you will see autotest execute in the first shell window. Here is the relevant output:

'Author should be stuff' FAILED
expected: true,
     got: false (using ==)
./spec/models/author_spec.rb:15:

Finished in 0.087639 seconds

2 examples, 1 failure

Now correct the test so that the offending line reads:

false.should==false

You’ll see how all the tests are rerun and there are no failures.
Edit the other test file, book_spec.rb, and add a passing test. You will see autotest execute only the tests in book_spec.rb.
However, if you add a failing test to book_spec.rb, then you correct it, you will see that this time autotest first executes the tests in book_spec.rb to check whether they all pass, then it goes on to execute all tests in all spec files.

It takes a little time to get used to the work style autotest offers, but it’s well worth the peace of mind to know that all your tests are passing.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

Comments