Rspec 代理上初始化器私有方法的测试覆盖率 class
Rspec test coverage on initializer private method on proxy class
我有以下代理 class,我正试图在其上达到 100% 的测试覆盖率。无论我如何构建此 Rspec 测试,simplecov 都会报告未涵盖 klass_constructor
方法。
我将如何更改这些规格以确保涵盖这些规格。我知道初始化器在救援中没有 return 值,即 false 或 true,但是这些 Rspec 存根会发生奇怪的事件。
代理Class代码
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
这并没有解决问题,但是
"#{@provider.capitalize}::Events::#{@provider.capitalize}Queue".safe_constantize
更干净。
我有以下代理 class,我正试图在其上达到 100% 的测试覆盖率。无论我如何构建此 Rspec 测试,simplecov 都会报告未涵盖 klass_constructor
方法。
我将如何更改这些规格以确保涵盖这些规格。我知道初始化器在救援中没有 return 值,即 false 或 true,但是这些 Rspec 存根会发生奇怪的事件。
代理Class代码
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
这并没有解决问题,但是
"#{@provider.capitalize}::Events::#{@provider.capitalize}Queue".safe_constantize
更干净。