0

I'm doing some tests here using Rspec and I would like to assure that the controller is calling the log method in some actions. I'm also using mocha.

I would like something like this:

it "update action should redirect when model is valid" do
    Tag.any_instance.stubs(:valid?).returns(true)
    put :update, :id => Tag.first
    controller.expects(:add_team_log).at_least_once
    response.should redirect_to(edit_admin_tag_url(assigns[:tag]))
  end

is there something to use as the 'controller' variable? I tried self, the controller class name...

Tiago
  • 2,966
  • 4
  • 33
  • 41
  • Would need to see your controller to code to understand what's going on here. Otherwise it just looks like it's failing your expectation. – Anthony Bishopric Mar 16 '11 at 22:50

2 Answers2

1

I just got helped with this. For testing controllers, you'd nest your specs inside a describe which names the controller. (The spec should also be in the Controllers folder)

describe ArticlesController do
  integrate_views
    describe "GET index" do
     ...
      it "update action should redirect when model is valid" do
         ...
        controller.expects(:add_team_log).at_least_once
    ...
     end
   end

end

Ed Jones
  • 653
  • 1
  • 4
  • 19
0

I think you want @controller instead of controller. Here's an example from my test suite:

it "delegates to the pricing web service" do
  isbn = "an_isbn"
  @controller.expects(:lookup)
    .with(isbn, anything)
    .returns({asin: "an_asin"})

  get :results, isbn: isbn
  assert_response :success
end
Kyle
  • 1,278
  • 6
  • 11