我要做的只是指定视图的单行辅助方法应该如何表现,但我不确定如果我在 Rails 中工作,我应该创建什么样的模拟对象(如果有的话)。
这是 events_helper.rb 的代码:
module EventsHelper
def filter_check_button_path
params[:filter].blank? ? '/images/buttons/bt_search_for_events.gif' : '/images/buttons/bt_refine_this_search.gif'
end
end
这是我的规范代码,在 events_helper_spec.rb 中:
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe EventsHelper do
#Delete this example and add some real ones or delete this file
it "should be included in the object returned by #helper" do
included_modules = (class << helper; self; end).send :included_modules
included_modules.should include(EventsHelper)
end
it "should return the 'refine image search' button if a search has been run" do
# mock up params hash
params = {}
params[:filter] = true
# create an instance of the class that should include EventsHelper by default, as the first test has verified (I think)
@event = Event.new
# call method to check output
@event.filter_check_button_path.should be('/images/buttons/bt_search_for_events.gif')
end
end
当我浏览了这里的文档 - http://rspec.info/rails/writing/views.html时,我对“模板”对象的来源感到困惑。
我也试过看这里,我认为这会给我指明正确的方向,但唉,没有骰子。http://jakescruggs.blogspot.com/2007/03/mockingstubbing-partials-and-helper.html
我在这里做错了什么?
谢谢,
克里斯