¿Qué estoy haciendo mal con esta prueba de ayuda rspec?
Pregunta
Todo lo que intento hacer es especificar cómo debe comportarse un método auxiliar de una línea para una vista, pero no estoy seguro de qué tipo de objeto simulado, (si lo hay) debería crear si estoy trabajando en Rieles.
Aquí está el 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
Y aquí está mi código de especificación, en 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
Cuando he revisado los documentos aquí, http://rspec.info/rails /writing/views.html , estoy desconcertado de dónde proviene el objeto 'plantilla'.
También intenté mirar aquí, lo que pensé que me orientaría en la dirección correcta, pero, por desgracia, no hay dados. http://jakescruggs.blogspot.com/2007/03/ mockingstubbing-partials-and-helper.html
¿Qué estoy haciendo mal aquí?
Gracias,
Chris
Solución
No está haciendo nada en esa especificación, solo está configurando un trozo, por lo que pasará, pero no ha probado nada.
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
Otros consejos
Estoy ejecutando mi prueba sin spec_helper (Ruby 1.9)
require_relative '../../app/helpers/users_helper'
describe 'UsersHelper' do
include UsersHelper
...
end
Ah,
Hice esta pregunta en la lista de correo rspec, y un alma amable (¡gracias Scott!) me explicó que hay un útil objeto auxiliar para esto, que deberías usar en su lugar, así:
Rails tiene su propia función auxiliar params = {: filter = > cierto} helper.stub! (: params) .and_return (params)
Ahora he actualizado el código así:
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
Y está funcionando. ¡Huzzah!