0

100% のテスト カバレッジを達成しようとしている次のプロキシ クラスがあります。この Rspec テストをどのように構築しても、simplecov はklass_constructorメソッドがカバーされていないと報告します。

これらの仕様を変更して、それが確実にカバーされるようにするにはどうすればよいでしょうか。イニシャライザが値を返さないことは知っています。つまり、レスキューでは false または true ですが、これらの Rspec スタブで奇妙なイベントが発生します。

プロキシ クラス コード

class IntegrationProvider
  include IntegrationError
  include IntegrationSettings

  def initialize(provider)
    @provider = provider
    return false unless valid_provider?(@provider)
    return false unless valid_klass_constructor?
    klass_constructor
  end

  def proxy_to
    @provider_klass
  end

  private

  def klass_constructor
    @provider_klass = "#{@provider.capitalize}::#{@provider.capitalize}Provider".safe_constantize
  end
end

付属の設定モジュール

module IntegrationSettings
  def valid_provider?(provider)
    supported_providers.include?(provider)
  end

  def valid_klass_constructor?
    klass_constructor
  end

  private

  def supported_providers
    Rails.cache.fetch('supported_providers', expires_in: 10.days) do
      Provider.where(active: true).pluck(:slug)
    end
  end
end

仕様

RSpec.describe IntegrationProvider, type: :integration do
  let!(:provider) { FactoryGirl.create(:provider, :active) }
  let!(:klass) { Provider }

  describe '#initialize' do
    it 'initializes' do
      stub_const("#{provider.slug.capitalize}::#{provider.slug.capitalize}Provider", klass)
      expect(IntegrationProvider.new(provider.slug)).to be_truthy
    end
  end

  describe '#proxy_to' do
    subject { described_class.new(provider.slug) }

    it 'is a proxy' do
      subject.instance_variable_set(:@provider_klass, klass)
      expect(subject.proxy_to).to eq(klass)
    end

    it 'inherits active record' do
      subject.instance_variable_set(:@provider_klass, klass)
      expect(subject.proxy_to.ancestors.include?(ActiveRecord::Base)).to be_truthy
    end
  end

  describe '#klass_constructor' do
    subject { described_class.new(provider.slug) }

    it 'true' do
      stub_const("#{provider.slug.capitalize}::#{provider.slug.capitalize}Provider", klass)
      expect(subject.send(:klass_constructor)).to be_truthy
    end

    it 'assignment' do
      stub_const("#{provider.slug.capitalize}::#{provider.slug.capitalize}Provider", klass)
      subject.send(:klass_constructor)
      expect(subject.instance_variable_get(:@provider_klass)).to eq(klass)
    end
  end
end
4

1 に答える 1

0

これで問題は解決しませんが、

"#{@provider.capitalize}::Events::#{@provider.capitalize}Queue".safe_constantize

よりきれいです。

于 2016-08-09T02:23:33.503 に答える