Testing Draper decorators with real helpers, including URL helpers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "spec_helper" | |
# :draper_with_helpers is necessary for the Draper objects | |
# to access real helpers, including URL helpers, in the spec. | |
describe MyDecorator, "#foo", :draper_with_helpers do | |
# This is necessary for the spec itself to use URL helpers | |
# like some_path(). | |
include Rails.application.routes.url_helpers | |
let(:item) { Item.new } | |
subject { MyDecorator.new(item).foo } | |
it { should include some_path(item) } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec.configure do |config| | |
config.treat_symbols_as_metadata_keys_with_true_values = true | |
# Make helpers, including link helpers, work with Draper in specs. | |
# https://github.com/jcasimir/draper/pull/49#issuecomment-4003191 | |
# NOTE: You may need to change ":all" to ":each" for this to work. See Gist comments. | |
config.before(:all, :draper_with_helpers) do | |
c = ApplicationController.new | |
c.request = ActionDispatch::TestRequest.new | |
c.set_current_view_context | |
end | |
end |
I too had to use :each
instead of :all
to make this work.
This is not working since this commit. This fork seems to do the trick.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm hitting the same problem as @santuxus where an
:each
filter was required.It looks like Draper's RSpec integration is clobbering the
:all
filter.I'm not sure if injecting the
TestRequest
in the around block is too heavy handed, but it seems to fix the problem for me.