RSpec / Rails - 如何测试ApplicationController中的方法是否被调用(当我测试子类控制器时)?

时间:2013-03-21 19:58:37

标签: ruby-on-rails rspec

标题说明了一切。

我有一个控制器动作,我在其中执行一些操作,然后调用ApplicationController中定义的方法。

如何测试它被调用?

controller.should_receive(:the_method_name)不起作用。
ApplicationController.should_receive(:the_method_name)不起作用。

什么是正确的语法?

感谢。

2 个答案:

答案 0 :(得分:1)

我认为您可以使用anonymous controller来测试ApplicationController,请查看文档。

答案 1 :(得分:0)

使用AnonymousController作为Gerep建议并调用controller.should_receive(:the_method_name)应该这样做。

测试中的位置对于让测试成功通过非常重要。举个例子:

describe 'my test' do
  before do
    controller.should_receive(:the_method)
    get :action
  end
  it { should render_template(:your_template) }
end

此示例应传递并测试:the_method的调用。