Skip to content

Commit

Permalink
test(cache): add specs for #[] and #[]=
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Apr 11, 2023
1 parent ef4a58a commit a9af67d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
11 changes: 4 additions & 7 deletions lib/convenient_service/support/cache/entities/caches/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,23 @@ def keygen(...)
end

##
# @param key [Object] Can be any type.
# @return [Object] Can be any type.
#
# @internal
# NOTE: `alias_method` is NOT used in order to have an ability to use `allow(cache).to receive(:read).with(key).and_call_original` for both `cache[key]` and `cache.read(key)` in RSpec.
#
def [](key)
read(key)
def [](...)
read(...)
end

##
# @param key [Object] Can be any type.
# @param value [Object] Can be any type.
# @return [Object] Can be any type.
#
# @internal
# NOTE: `alias_method` is NOT used in order to have an ability to use `allow(cache).to receive(:write).with(key, value).and_call_original` for both `cache[key] = value` and `cache.write(key, value)` in RSpec.
#
def []=(key, value)
write(key, value)
def []=(...)
write(...)
end

##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

# rubocop:disable RSpec/NestedGroups
RSpec.describe ConvenientService::Support::Cache::Entities::Caches::Array do
include ConvenientService::RSpec::Matchers::DelegateTo

example_group "inheritance" do
include ConvenientService::RSpec::Matchers::BeDescendantOf

Expand Down Expand Up @@ -263,6 +265,29 @@
end
end

describe "#[]" do
let(:key) { :foo }

specify do
expect { cache[key] }
.to delegate_to(cache, :read)
.with_arguments(key)
.and_return_its_value
end
end

describe "#[]=" do
let(:key) { :foo }
let(:value) { :foo }

specify do
expect { cache[key] = value }
.to delegate_to(cache, :write)
.with_arguments(key, value)
.and_return_its_value
end
end

describe "#scope" do
include ConvenientService::RSpec::Matchers::CacheItsValue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@
.and_return_its_value
end
end

##
# NOTE: Tested by descendants.
#
# describe "#[]" do
# end

##
# NOTE: Tested by descendants.
#
# describe "#[]=" do
# end
end
end
# rubocop:enable RSpec/NestedGroups
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

# rubocop:disable RSpec/NestedGroups
RSpec.describe ConvenientService::Support::Cache::Entities::Caches::Hash do
include ConvenientService::RSpec::Matchers::DelegateTo

example_group "inheritance" do
include ConvenientService::RSpec::Matchers::BeDescendantOf

Expand Down Expand Up @@ -263,6 +265,29 @@
end
end

describe "#[]" do
let(:key) { :foo }

specify do
expect { cache[key] }
.to delegate_to(cache, :read)
.with_arguments(key)
.and_return_its_value
end
end

describe "#[]=" do
let(:key) { :foo }
let(:value) { :foo }

specify do
expect { cache[key] = value }
.to delegate_to(cache, :write)
.with_arguments(key, value)
.and_return_its_value
end
end

describe "#scope" do
include ConvenientService::RSpec::Matchers::CacheItsValue

Expand Down

0 comments on commit a9af67d

Please sign in to comment.