在模板助手规范中存根控制器助手方法 [英] Stub a controller helper method in a template helper spec

查看:54
本文介绍了在模板助手规范中存根控制器助手方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ApplicationController 使用helper_method :sort_direction 向视图模板公开一个方法(例如sort_direction).然后我在视图助手 (application_helper.rb) 的另一种方法(例如 sort_link)中使用此方法.

My ApplicationController exposes a method (e.g. sort_direction) to the view templates by using helper_method :sort_direction. I then use this method in another method (e.g. sort_link) of a view helper (application_helper.rb).

当使用 RSpec(在 application_helper_spec.rb 中)测试 sort_link 方法时,我必须存根 sort_direction 因为测试似乎完全独立运行从控制器(从而通过其到视图模板公开的方法).

When testing the sort_link method with RSpec (in application_helper_spec.rb) I have to stub sort_direction as the test seems to run complete independent from the controllers (and thereby by its to the view templates exposed methods).

不幸的是,我不知道如何存根控制器的 sort_direction 方法.我总是得到未定义的方法".

Unfortunately I could not find out how to stub that sort_direction method of the controller. I always get "undefined method".

这是我到目前为止所尝试的(在 application_helper_spec.rb 中):

Here is what I tried so far (inside application_helper_spec.rb):

helper.stub(:sort_direction)
controller.stub(:sort_direction)
view.stub(:sort_direction)
self.stub(:sort_direction)

有什么建议我可以存根该方法吗?

Any suggestions how I can stub that method?

这是我得到的错误:

NoMethodError:
       undefined method `sort_direction' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xb641434c>

推荐答案

David Chelimsky 在这里解决了这个问题:http://groups.google.com/group/rspec/browse_thread/thread/cc44ca12c6816053

David Chelimsky solved that problem here: http://groups.google.com/group/rspec/browse_thread/thread/cc44ca12c6816053

只需在规范中调用 helper 对象上的所有方法:

Simply call in the spec all methods on the helper object:

it "should work" do
   helper.stub(:sort_direction)
   helper.sort_link(...).should == ...
end

这篇关于在模板助手规范中存根控制器助手方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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