3

私がやろうとしているのは、ビューの 1 行のヘルパー メソッドがどのように動作するかを指定することだけですが、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

ここで何が間違っていますか?

ありがとう、

クリス

4

3 に答える 3

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
于 2009-10-02T13:59:43.680 に答える
1

spec_helper なしでテストを実行しています (Ruby 1.9)

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

describe 'UsersHelper' do 
  include UsersHelper

  ...
end
于 2012-05-13T18:48:25.013 に答える