문제

내가하려는 것은보기를위한 원한 도우미 방법이 어떻게 행동 해야하는지 사양이지만, 레일에서 일할 때 어떤 종류의 모의 개체 (있는 경우)는 확실하지 않습니다.

다음은 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

다음은 이벤트 _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

여기서 내가 뭘 잘못하고 있니?

감사,

크리스

도움이 되었습니까?

해결책

당신은 그 사양에서 아무것도하지 않고 스터브를 설정하기 만하면 통과하지만 아무것도 테스트하지 않았습니다.

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

다른 팁

Spec_Helper없이 테스트를 실행하고 있습니다 (Ruby 1.9)

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

describe 'UsersHelper' do 
  include UsersHelper

  ...
end

아,

나는 RSPEC 메일 링리스트 에이 질문을했고, 한 종류의 영혼 (Scott에게 감사합니다!)은 나에게 편리한 도우미 대상이 있다고 설명했다.

Rails에는 자체 도우미 기능이 있습니다. Params = {: Filter => True} Helper.stub! (: Params). 및_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

그리고 그것은 효과가 있습니다. 허자!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top