Skip to content

RSpec Tips & Tricks

antislice edited this page Mar 18, 2013 · 9 revisions

RSpec is the unit testing framework that we're using.

Running tests

/HelloRubyTuesdays> rspec spec/                          # runs all the tests
/HelloRubyTuesdays> rspec spec/model/game_spec.rb        # runs just the tests for the Game model
/HelloRubyTuesdays> rspec spec/model/game_spec.rb:3      # runs the unit test in the Game spec that's at line 3

Basic test structure/methods

Here's an example test from the Michael Hartl tutorial:

describe User do

  before { @user = User.new(name: "Example User", email: "user@example.com") }

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
end

Stumbling blocks

If your controller test fails with this message:

ActionView::MissingTemplate: Missing template games/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :haml]}. Searched in:
  * "#<RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator:0x007ff304011108>"

then the controller method it is trying to test is expected to have a redirect method in it. Take a look at the rails docs for some example redirects.

Clone this wiki locally