Skip to content

Instantly share code, notes, and snippets.

@SamSamskies
Forked from ndelage/testing tips.md
Created May 16, 2013 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamSamskies/5588938 to your computer and use it in GitHub Desktop.
Save SamSamskies/5588938 to your computer and use it in GitHub Desktop.

Testing Tips & Tricks

Run a single test

In a large application, running all the tests at once using rake spec can make it difficult to check the output of the test you're working on. Especially if you have many failing or pending tests. Run a single test by running rspec:

rspec spec/models/account.rb

You can be even more specific and run a specific example from a test file. For example, given the following tests:

11: describe "#deposit!" do
12:  it "adds value to the balance" do
13:     expect{account.deposit!(10)}.to change{account.balance}.by(10)
14:   end
15:   it "returns the balance" do
16:     account.deposit!(10).should == account.balance
17:   end
18: end

rspec account_spect.rb:15 will only run the test it "returns the balance"

Run all the tests frequently and always before making any commits

Running a specfic example file or test helps you focus, but don't forget to run the entire test suite with rake spec before making any commits. While the test you're might be passing, it's very possible your changes might have broken another test. Better to get that feedback immediately, rather than later.

Consult the log

When you run any rails application (the webserver, tests or rake tasks) ouput from the running code is saved to a log file. There are log files per environment: log/development.log, log/test.log and log/production.log

Take a moment to open up one of these log files in your editor and take a look at its contents. To keep an eye on what's new in your log files, use the *nix tool tail. Try running the following on your terminal:

tail -f log/test.log

Now run a test. Curious what -f means? Check the man page for the tail utility: man tail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment