Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access helper methods in my decorator spec files using Draper 0.14.0

Currently in my spec/decorators/product_decorator_spec.rb, I have the following:

require 'spec_helper'

describe ProductDecorator do
  let(:product) { FactoryGirl.create(:product) }

  subject do
    ProductDecorator.first
  end

  before do
    product
  end

  it 'should render the name attribute with a link to the product page' do
    subject.name.should == h.link_to(product.name, 'test')
  end
end

When I run my spec I get the following:

F.....

Failures:

  1) ProductDecorator should render the name attribute with a link to the product page
     Failure/Error: subject.name.should == h.link_to(product.name, 'resr')
      NameError:
       undefined local variable or method `h' for #<RSpec::Core::ExampleGroup::Nested_2:0x007fbbf212c8b0>
     # ./spec/decorators/product_decorator_spec.rb:15:in `block (2 levels) in <top (required)>'

Finished in 0.98531 seconds
6 examples, 1 failure

Failed examples:

rspec ./spec/decorators/product_decorator_spec.rb:14 # ProductDecorator should render the name attribute with a link to the product page

According to the documentation, specs placed in the decorator folder should have access to the helper method, however my spec does not. I've also tried manually tagging my specs, but doesn't seem to have any effect.

Thanks for looking.

like image 629
jklina Avatar asked Dec 26 '22 22:12

jklina


2 Answers

if you want to access the helper, you can do it via your_decorator.h.link_to.

when you are setting the subject, you will need to make sure that the thing you are calling will get routed to the helper, there is nothing injected into your rspec example!

in your example it would be subject.h.link_to for calling a helper method.

i also think that there are a lot of wired things in your spec. your usage of let, subject and before are kind of disturbing for me...

here is a nice writeup about how to write clean rspec: http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/

like image 120
phoet Avatar answered Dec 30 '22 07:12

phoet


I've encountered the same issue, where calling helper methods on the decorator's helper proxy (.h) doesn't work in test (in Draper 1.3). I ended up working around it with this, though I'm not very pleased with it:

my_decorated_object.h.extend ApplicationHelper

Your mileage may vary depending on how many controller features you access in your helper.

like image 25
Pathogen Avatar answered Dec 30 '22 08:12

Pathogen