-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added coverage report
- Loading branch information
Showing
10 changed files
with
466 additions
and
716 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ pkg/* | |
*DS_Store | ||
.ruby-version | ||
.tool-versions | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,92 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe CachedResource do | ||
before do | ||
class BaseThing < ActiveResource::Base | ||
RSpec.describe CachedResource::Model do | ||
let(:logger) { double(:Logger, error: nil)} | ||
let(:dummy_class) do | ||
Class.new(ActiveResource::Base) do | ||
include CachedResource::Model | ||
end | ||
end | ||
|
||
describe '.cached_resource' do | ||
context 'when cached resource is not set up' do | ||
it 'initializes and returns a new cached resource configuration' do | ||
options = { logger: logger } | ||
allow(CachedResource::Configuration).to receive(:new).with(options).and_call_original | ||
|
||
class FirstChildThing < BaseThing | ||
self.site = 'http://api.first-child-thing.com' | ||
cached_resource | ||
cached_resource = dummy_class.cached_resource(options) | ||
|
||
expect(cached_resource).to be_a(CachedResource::Configuration) | ||
expect(dummy_class.instance_variable_get(:@cached_resource)).to eq(cached_resource) | ||
expect(CachedResource::Configuration).to have_received(:new).with(options) | ||
end | ||
end | ||
|
||
class SecondChildThing < BaseThing | ||
self.site = 'http://api.second-child-thing.com' | ||
context 'when cached resource is already set up' do | ||
it 'returns the existing cached resource configuration' do | ||
existing_config = CachedResource::Configuration.new | ||
dummy_class.instance_variable_set(:@cached_resource, existing_config) | ||
|
||
cached_resource = dummy_class.cached_resource | ||
|
||
expect(cached_resource).to eq(existing_config) | ||
end | ||
end | ||
end | ||
|
||
after do | ||
[:BaseThing, :FirstChildThing, :SecondChildThing].each do |klass| | ||
Object.send(:remove_const, klass) | ||
describe '.setup_cached_resource!' do | ||
it 'creates a new cached resource configuration' do | ||
options = { logger: logger } | ||
allow(CachedResource::Configuration).to receive(:new).with(options).and_call_original | ||
|
||
dummy_class.setup_cached_resource!(options) | ||
|
||
expect(dummy_class.instance_variable_get(:@cached_resource)).to be_a(CachedResource::Configuration) | ||
expect(CachedResource::Configuration).to have_received(:new).with(options) | ||
end | ||
|
||
context 'when concurrent_write is enabled' do | ||
before do | ||
allow(CachedResource::Configuration).to receive(:new).and_return( | ||
double(concurrent_write: true, logger: logger) | ||
) | ||
end | ||
|
||
it 'requires concurrent/promise' do | ||
expect { dummy_class.setup_cached_resource!(concurrent_write: true) }.not_to raise_error(LoadError) | ||
end | ||
|
||
context 'When "concurrent/promise" is not installed' do | ||
before do | ||
allow(dummy_class).to receive(:require).with("concurrent/promise").and_raise(LoadError) | ||
end | ||
|
||
it 'requires concurrent/promise' do | ||
expect(logger).to receive(:error).with( | ||
"`concurrent_write` option is enabled, but `concurrent-ruby` is not an installed dependency" | ||
).once | ||
expect { dummy_class.setup_cached_resource!(concurrent_write: true) }.to raise_error(LoadError) | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe '.inherited' do | ||
it 'should include descendants when calling .descendants' do | ||
BaseThing.descendants.sort_by { |klass| klass.name }.should == [FirstChildThing, SecondChildThing] | ||
let(:child_class) { Class.new(dummy_class) } | ||
|
||
context 'when cached resource is defined in superclass' do | ||
it 'copies the cached resource configuration to the subclass' do | ||
existing_config = CachedResource::Configuration.new | ||
dummy_class.instance_variable_set(:@cached_resource, existing_config) | ||
|
||
expect(child_class.cached_resource).to eq(existing_config) | ||
end | ||
end | ||
|
||
context 'when cached resource is not defined in superclass' do | ||
it 'does not set cached resource configuration in the subclass' do | ||
expect(child_class.instance_variable_defined?(:@cached_resource)).to be_falsey | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.