What am I doing wrong with this rspec helper test?

2019-07-07 00:04发布

All I'm trying to do is spec how a one line helper method for a view should behave, but I'm not sure what kind of mock object, (if any) I should be creating if I'm working in Rails.

Here's the code for 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

And here's my spec code, in 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

When I've looked through the docs here - http://rspec.info/rails/writing/views.html, I'm mystified as to where the 'template' object comes from.

I've also tried looking here, which I thought would point me in the right direction, but alas, no dice. http://jakescruggs.blogspot.com/2007/03/mockingstubbing-partials-and-helper.html

What am I doing wrong here?

Thanks,

Chris

3条回答
做个烂人
2楼-- · 2019-07-07 00:31

Ah,

I asked this question on the rspec mailing list, and one kind soul (thanks Scott!) explained to me that there's a handy helper object for this, that you should use instead, like so:

Rails has its own helper function params = {:filter => true} helper.stub!(:params).and_return(params)

I've now updated the code like so:

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

And it's working. Huzzah!

查看更多
够拽才男人
3楼-- · 2019-07-07 00:50

You are not doing anything in that spec, just setting a stub, so it will pass, but hasn't tested anything.

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
查看更多
淡お忘
4楼-- · 2019-07-07 00:53

I'm running my test without spec_helper (Ruby 1.9)

require_relative '../../app/helpers/users_helper'

describe 'UsersHelper' do 
  include UsersHelper

  ...
end
查看更多
登录 后发表回答