Question

I try test my view with Rspec. In my view, I have a Decorator generate by Draper. This Decorator is expose by decent_exposure gem.

I create my rspec test like that :

require 'spec_helper'

describe "boats/show.html.slim" do

  let(:boat_decorate) { BoatDecorator.new(get_boat) }
  let(:search) { Search.new }

  before do
    view.stub(:boat_decorate) { boat_decorate }
    view.stub(:search) { search }
    render :template => 'boats/show.html.slim'
  end 

  it 'should see titlte' do
    rendered.should have_selector(:h1, :content => boat_decorate.title)
  end                                                                                                                                                                                
end

In my stub of my helper I generate the Draper Decorator. In this Decorator I have method to call some helper link_to.

class BoatDecorator < ApplicationDecorator

  decorates :boat

  def region_link
    h.link_to region_name, '#', :title => region_name                                                                                                                                
  end

end

But If I launch this test I have an error :

  1) boats/show.html.slim should see titlte
     Failure/Error: render :template => 'boats/show.html.slim'
     ActionView::Template::Error:
       undefined method `link_to' for nil:NilClass

I don't want stub all helper call by my Decorator. So how can I do ?

Was it helpful?

Solution

We need do like a Decorator spec and add in setup : ApplicationController.new.set_current_view_context

require 'spec_helper'

describe "boats/show.html.slim" do

  let(:boat_decorate) { BoatDecorator.new(get_boat) }
  let(:search) { Search.new }

  before do
    ApplicationController.new.set_current_view_context
    view.stub(:boat_decorate) { boat_decorate }
    view.stub(:search) { search }
    render :template => 'boats/show.html.slim'
  end 

  it 'should see titlte' do
    rendered.should have_selector(:h1, :content => boat_decorate.title)
  end                                                                                                                                                                                
end

OTHER TIPS

Another approach might be testing the decorator class in isolation with rspec and capybara. Here is an example code showing using this approach .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top