views:

1037

answers:

2

I'm trying to understand why this test is failing. (I'm kind of new to testing.) I'm using the built-in Rails testing framework with the addition of the Shoulda gem.

The test:

require 'shoulda'

context "on GET to :new" do

  setup do
    get(:new)
  end

  should_render_template :new
  should_not_set_the_flash

end

Fails:

1) Failure:
test: on GET to :new should render template :new. (SessionsControllerTest)
[/usr/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.0.6/lib/shoulda/controller   /macros.rb:220:in `__bind_1233882600_699194'
/usr/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.0.6/lib/shoulda/context.rb:254:in `call'
/usr/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.0.6/lib/shoulda/context.rb:254:in `test: on GET to :new should render template :new. '
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:94:in `__send__'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:94:in `run']:
expecting <"new"> but rendering with <"">

2 tests, 2 assertions, 1 failures, 0 errors

But if I run it on the console with app.get '/sessions/new' it works fine with no error.

And the "new" template renders as expected in the browser.

I'm using Haml. Maybe that's causing a problem. My template is called "new.html.haml".

+2  A: 

The failure says that no template has been rendered. Try to make sure you are not being redirected with

should_respond_with :success

and assert the contents of the @response.body just to see what's been sent back

# This will fail but should give you some clue about what was sent back.
should "sent something back in the body" do
  assert_match 'boohooo', @response.body
end

You can also assert a specific template, so you can give it a shot, too:

should_render_template "new.html.haml"

However, I don't suspect HAML being the cause of your problem.

Milan Novota
Thanks. The call to assert_match you suggested solved it. I had forgotten about a before filter that was messing things up.
Ethan
A: 

I was having a similar problem with Shoulda, Clearance, and Rails 2.3.2. I think I resolved the issue by modifying how shoulda and clearance use 'assert_template'. Apparently there is an open bug in Rails related to this.

Please see this thread for more info and my diff of the changes (one line change in both Clearance and Shoulda).

http://groups.google.com/group/shoulda/browse_thread/thread/8c0a66c80ff4fd76

Cheers,

Glenn