Ruby on rails 在RSPEC功能测试中使用mocha作为控制器

Ruby on rails 在RSPEC功能测试中使用mocha作为控制器,ruby-on-rails,controller,rspec,functional-testing,mocha.js,Ruby On Rails,Controller,Rspec,Functional Testing,Mocha.js,我在这里使用Rspec进行一些测试,我想确保控制器在某些操作中调用log方法。我也在用摩卡咖啡 我想要这样的东西: 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_

我在这里使用Rspec进行一些测试,我想确保控制器在某些操作中调用log方法。我也在用摩卡咖啡

我想要这样的东西:

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

有什么东西可用作“控制器”变量吗?我试过self,控制器类名…

我想你想要的是@controller而不是controller。下面是我的测试套件中的一个示例:

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

我刚得到帮助。对于测试控制器,您需要将规格嵌套在一个描述中,该描述命名控制器。(规范也应位于控制器文件夹中)


end

需要查看控制器以编写代码以了解此处发生的情况。否则,它看起来就像是辜负了你的期望。
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