我对这个rspec助手测试做错了什么?

时间:2009-09-17 19:25:07

标签: testing rspec helpers

我想要做的就是规范视图的单行辅助方法应该如何表现,但我不确定如果我在工作的话,我应该创建什么样的模拟对象(如果有的话)轨。

以下是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

我在这里做错了什么?

谢谢,

克里斯

3 个答案:

答案 0 :(得分:10)

你没有在那个规范中做任何事情,只是设置一个存根,所以它会通过,但没有测试任何东西。

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe EventsHelper do
 it "should return the 'refine image search' button if a search has been run" do
  # mock up params hash
  params = {:filter => true}

  helper.stub!(:params).and_return(params)
  helper.filter_check_button_path.should eql('/images/buttons/bt_search_for_events.gif')
 end
end

答案 1 :(得分:1)

我在没有spec_helper(Ruby 1.9)

的情况下运行我的测试
require_relative '../../app/helpers/users_helper'

describe 'UsersHelper' do 
  include UsersHelper

  ...
end

答案 2 :(得分:-2)

啊,

我在rspec邮件列表上问了这个问题,一个灵魂(感谢Scott!)向我解释说有一个方便的帮助对象,你应该使用它,如下:

  

Rails有自己的辅助函数   params = {:filter =&gt;真正}   helper.stub!(:PARAMS).and_return(PARAMS)

我现在更新了代码:

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

    helper.stub!(:filter_check_button_path).and_return('/images/buttons/bt_search_for_events.gif')
  end

end

它正在发挥作用。好哇!