10

I am attempting to write specs for the individual functions in my decorators. I have specs for my helpers like the following (this is just an example):

book_helper.rb

module BookHelper
  def heading_title
    @book.name[0..200]
  end
end

book_helper_spec.rb

require 'spec_helper'

describe BookHelper do
  subject { FactoryGirl.build(:book) }

  it 'limits title to 200 characters' do
    title = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium.'
    subject.name = title
    subject.save
    @book = subject
    expect(heading_title).to eq(title[0..200])
  end
end

Given the following decorator, how can I write a spec for the function?

book_decorator.rb

class BookDecorator < Draper::Decorator
  delegate_all

  def display_days
    model.months_to_display * 30
  end
end
Zack
  • 2,377
  • 4
  • 25
  • 51

2 Answers2

14

For your sample, I'd try with something like:

require 'spec_helper'

describe BookDecorator do
  let(:book) { FactoryGirl.build_stubbed(:book).decorate }

  it 'returns the displayed days' do
    expect(book.display_days).to eq('600')
  end

end
Alter Lagos
  • 12,090
  • 1
  • 70
  • 92
  • This won't work if the code under test expects @book to be non-decorated, which is the recommended way to do things: https://github.com/drapergem/draper#when-to-decorate-objects – jelder Sep 21 '16 at 21:18
-3

Just use .decorate for the generated entity (using FactoryGirl or Faker)