在 RSPEC 功能测试中使用 mocha 作为控制器
我在这里使用 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,控制器类名......
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚得到了这方面的帮助。为了测试控制器,您可以将规范嵌套在命名控制器的描述中。 (规范也应该位于 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)
end
我认为你想要 @controller 而不是控制器。这是我的测试套件中的一个示例:
I think you want @controller instead of controller. Here's an example from my test suite: