Skip to content

Instantly share code, notes, and snippets.

Created February 16, 2012 18:55
Show Gist options
  • Save henrik/1847005 to your computer and use it in GitHub Desktop.
Save henrik/1847005 to your computer and use it in GitHub Desktop.
Testing Draper decorators with real helpers, including URL helpers.
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) { }
subject { }
it { should include some_path(item) }
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
# Make helpers, including link helpers, work with Draper in specs.
# NOTE: You may need to change ":all" to ":each" for this to work. See Gist comments.
config.before(:all, :draper_with_helpers) do
c =
c.request =
Copy link

bdotdub commented Feb 16, 2012

Thank you!

Copy link

niuage commented Mar 19, 2012

Thanks :)

Copy link

Thank you! I don't know why, but I had to change 'before(:all)' to 'before(:each)' - only then it worked.

Copy link

niuage commented Mar 23, 2012

I think there is an underlying problem with your setup santuxus, it works fine with before :all. It will make your tests slower if you have to do that for every tests.

Copy link

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.

config.around do |example|
  if :decorator == example.metadata[:type]

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.

Copy link

zetetic commented May 11, 2012

I too had to use :each instead of :all to make this work.

Copy link

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