在RSPEC的功能测试中将Mocha用于控制器 [英] Using mocha for controller in functional test with RSPEC

查看:72
本文介绍了在RSPEC的功能测试中将Mocha用于控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我想要这样的东西:

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,控制器类名...

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

推荐答案

我刚刚得到了帮助.对于测试控制器,您可以将规格嵌套在描述控制器名称的描述中.(规格也应位于Controllers文件夹中)

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

结束

这篇关于在RSPEC的功能测试中将Mocha用于控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆