views:

262

answers:

2

Hi,

I'm using Test::Unit with shoulda to test a controller.

Since I'm just testing the controller I dont wanna the view to be rendered.

I'm stubbing some objects, some errors are throw when the view is rendered, but the test shouldn't fail, because the controller is correct.

So, theres any way to disable the rendering of a template/view, from my tests?

I heard that rSpec works like that.

A: 

You shouldn't really be seeing any view in your tests. Can you post up your failing test code? The controller (functional) tests should only be checking that a particular action is occurring when your action is called. I.e it should check that it renders the correct view or redirects to a different action. You can also check the setup of the flash or other variables for the view. Is this the type of this you are testing for?

Here is a good example of testing a show action with a get request taken from the shoulda docs:

class UsersControllerTest < Test::Unit::TestCase
  context "on GET to :show" do
    setup { get :show, :id => 1 }

    should_assign_to :user
    should_respond_with :success
    should_render_template :show
    should_not_set_the_flash

    should "do something else really cool" do
      assert_equal 1, assigns(:user).id
    end
  end
end

Maybe take a look at rails guides which is pretty good too.

tsdbrown
+2  A: 

If you're using Mocha, it's easy. Add this to your individual test or your setup method:

@controller.expects(:render)

If you're not, well, use Mocha.

gem install mocha

Then in your test_helper.rb

require 'mocha'
Brian Hogan