Using mocha for controller in functional test with RSPEC

[亡魂溺海] 提交于 2021-01-29 17:55:15

问题


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...


回答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




回答2:


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


来源:https://stackoverflow.com/questions/5332170/using-mocha-for-controller-in-functional-test-with-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!