I have the following proxy class that I am trying to hit 100% test coverage on. No matter how I construct this Rspec test simplecov reports the klass_constructor
method as not covered.
How would I alter these specs to make sure that is covered. I know initializers do not return values i.e. false or true in the rescues but weird events happen with these Rspec stubs.
Proxy Class Code
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
Included Settings Module
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
Spec
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