O que estou fazendo de errado com este teste ajudante rspec?
Pergunta
Tudo o que estou tentando fazer é especificação como um método auxiliar uma linha para uma visão deve se comportar, mas não tenho certeza que tipo de simulação objeto, (se houver) que eu deveria estar criando se eu estou trabalhando em Rails.
Aqui está o código para 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
E aqui está o meu código de spec, em 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
Quando eu olhei através dos docs aqui - http://rspec.info/rails /writing/views.html , estou perplexo quanto ao local onde o objeto 'modelo' vem.
Eu também tentei olhar aqui, que eu pensei que iria me aponte na direção certa, mas, infelizmente, não dados. http://jakescruggs.blogspot.com/2007/03/ mockingstubbing-parciais-and-helper.html
O que estou fazendo de errado aqui?
Obrigado,
Chris
Solução
Você não está fazendo nada em que spec, apenas a criação de um stub, por isso vai passar, mas não testou qualquer coisa.
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
Outras dicas
Eu estou correndo meu teste sem spec_helper (Ruby 1.9)
require_relative '../../app/helpers/users_helper'
describe 'UsersHelper' do
include UsersHelper
...
end
Ah,
Eu fiz esta pergunta na lista de discussão rspec, e um tipo alma (graças Scott!) Explicou-me que há um objeto auxiliar útil para isso, que você deve usar em vez disso, como assim:
Rails tem a sua própria função auxiliar params = {: filter => true} helper.stub! (: params) .and_return (params)
Eu já atualizou o código assim:
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
e está funcionando. Huzzah!