Qu'est-ce que je fais de mal avec ce test d'assistance rspec?
Question
Tout ce que j'essaie de faire, c'est de spécifier le comportement d'une méthode d'assistance à une ligne pour une vue, mais je ne suis pas sûr du type d'objet fictif que je devrais créer, le cas échéant, si je travaille dans Rails.
Voici le code pour 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
Et voici mon code de spécification, dans 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
Lorsque j'ai consulté les documents ici - http://rspec.info/rails /writing/views.html , je ne suis pas sûr de la provenance de l’objet 'modèle'.
J'ai aussi essayé de regarder ici, ce qui, je pense, m'indiquerait la bonne direction, mais hélas, pas de dés. http://jakescruggs.blogspot.com/2007/03/ mockingstubbing-partiials-and-helper.html
Qu'est-ce que je fais mal ici?
Merci,
Chris
La solution
Vous ne faites rien dans cette spécification, vous définissez juste un stub, donc ça va passer, mais rien n'a été testé.
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
Autres conseils
Je lance mon test sans spec_helper (Ruby 1.9)
require_relative '../../app/helpers/users_helper'
describe 'UsersHelper' do
include UsersHelper
...
end
Ah,
J'ai posé cette question sur la liste de diffusion rspec, et une gentille âme (merci Scott!) m'a expliqué qu'il existe un objet d'aide très utile pour cela, que vous devriez utiliser à la place, comme ceci:
Rails a sa propre fonction d'assistance params = {: filter = > vrai} helper.stub! (: params) .and_return (params)
J'ai maintenant mis à jour le code comme suit:
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
Et ça marche. Huzzah!