Testing

Adding Unit Tests

When you generate a model, you get an _spec.rb file generated along with your controller. You would get the following for a model named “Person”.

Generating with model generator:
....
[ADDED]  app/test/person_spec.rb

This file contains tests for your controller and is in the mspec format: rubyspec.org

describe "Person" do
  #this test always fails, you really should have tests!

  it "should have tests" do
    true.should == false
  end
end

We use this test format internally as well. You can see our specs for the core framework here which use many functions of mspec.

To run these tests however, you need the testing framework to be included in your app. To add this, you would run the rhogen task in your application folder:

$ rhogen spec

You will then see the mspec framework added to your application:

Generating with spec generator:
     [ADDED]  app/SpecRunner
     [ADDED]  app/mspec.rb
     [ADDED]  app/spec_runner.rb

Finally, add the fileutils and mspec extensions to your build.yml:

extensions: ["fileutils", "mspec"]

You are now ready to run the tests. Simply add a link to the SpecRunner controller, and you will get a summary of number of passing/failing tests

In your index.erb:

<li><a href="SpecRunner">Run tests</a></li>

A summary of the results will be displayed on the screen.

Detailed results will be displayed in your rholog.txt:

I 01/15/2010 16:36:33 b0185000                  APP| FAIL: Product - Expected true
 to equal false

apps/app/mspec/expectations/expectations.rb:15:in `fail_with'
apps/app/mspec/matchers/base.rb:8:in `=='
apps/app/Product/product_spec.rb:5:in `block (2 levels) in <main>'
...

And finally, a summary will be printed in rholog.txt as well:

I 01/15/2010 16:36:33 b0185000                  APP| ***Total:  3
I 01/15/2010 16:36:33 b0185000                  APP| ***Passed: 1
I 01/15/2010 16:36:33 b0185000                  APP| ***Failed: 2

Disabling tests

When you are ready to do a production build of your application, change build.yml’s build property to ‘release’ and the specs will not be included in the binary:

...
vendor: Acme, Inc.
build: release
...
Back to Top