【问题标题】:How to spec draper decorators with rspec.如何使用 rspec 指定 draper 装饰器。
【发布时间】:2014-11-10 00:14:21
【问题描述】:

我正在尝试为我的装饰器中的各个功能编写规范。我的助手有如下规格(这只是一个示例):

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

【问题讨论】:

    标签: ruby-on-rails rspec draper


    【解决方案1】:

    对于您的示例,我会尝试类似:

    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
    

    【讨论】:

    【解决方案2】:

    只需对生成的实体使用 .decorate(使用 FactoryGirl 或 Faker)

    【讨论】:

    • 你的例子是有效的,因为Alter Lagos,也回答了,;D
    • 你是什么意思?!没听懂
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多