简体   繁体   English

在RSPEC的功能测试中将Mocha用于控制器

[英]Using mocha for controller in functional test with RSPEC

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. 我在这里使用Rspec进行一些测试,我想确保控制器在某些操作中正在调用log方法。 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... 我尝试了self,控制器类名...

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) (规格也应位于Controllers文件夹中)

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 结束

I think you want @controller instead of controller. 我想你想要@controller而不是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM