[英]RSpec / Rails - How do I test that a method in ApplicationController is called (when I'm testing a subclassed controller)?
Title says it all. 标题说明了一切。
I have a controller action in which I do some stuff, then call a method that is defined in ApplicationController
. 我有一个控制器动作,我在其中执行一些操作,然后调用
ApplicationController
定义的方法。
How do I test that it was called? 我如何测试它被调用?
controller.should_receive(:the_method_name)
doesn't work. controller.should_receive(:the_method_name)
不起作用。
ApplicationController.should_receive(:the_method_name)
doesn't work. ApplicationController.should_receive(:the_method_name)
不起作用。
What's the proper syntax? 什么是正确的语法?
Thanks. 谢谢。
我想你可以使用匿名控制器来测试你的ApplicationController,查看文档。
Using the AnonymousController as Gerep suggested and calling controller.should_receive(:the_method_name)
should do it. 使用AnonymousController作为Gerep建议并调用
controller.should_receive(:the_method_name)
应该这样做。
The position within the test is important to let the test pass successfully. 测试中的位置对于让测试成功通过非常重要。 To give you an example:
举个例子:
describe 'my test' do
before do
controller.should_receive(:the_method)
get :action
end
it { should render_template(:your_template) }
end
This example should pass and tests also the call of :the_method
. 这个例子应该传递并测试调用
:the_method
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.