Rafael Mueller
Rafael Mueller

Reputation: 6083

Disable render when testing a controller

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.

Upvotes: 2

Views: 1626

Answers (2)

Brian Hogan
Brian Hogan

Reputation: 3043

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'

Upvotes: 6

tsdbrown
tsdbrown

Reputation: 5058

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.

Upvotes: -1

Related Questions