我正在尝试为装饰器中的各个函数编写规范。我有我的助手的规格如下(这只是一个例子):
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
鉴于以下装饰器,我该如何为该函数编写规范?
book_decorator.rb
class BookDecorator < Draper::Decorator
delegate_all
def display_days
model.months_to_display * 30
end
end
答案 0 :(得分:12)
对于你的样本,我会尝试使用类似的东西:
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
答案 1 :(得分:-2)
只需对生成的实体使用.decorate(使用FactoryGirl或Faker)