Skip to content

Commit

Permalink
Improve spec for cache_key_prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
jlurena committed Jul 11, 2024
1 parent a5de05c commit 170ae8e
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions spec/cached_resource/caching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,6 @@ def read_from_cache(key, model = Thing)
proc { "prefix123/" },
proc { "prefix123/" },
proc { "prefix123/" },
proc { "prefix456/" },
proc { "prefix456/" },
proc { "prefix456/" }
)
end
Expand All @@ -302,6 +300,36 @@ def read_from_cache(key, model = Thing)
result = Thing.find(1, reload: true)
expect(read_from_cache("prefix456/thing/1")).to eq(result)
end

context "When subsequent times, callable is not nil" do
before do
allow(thing_cached_resource).to receive(:cache_key_prefix)
.and_return(
nil,
proc { "prefix123/" },
proc { "prefix123/" },
proc { "prefix123/" },
proc { "prefix456/" }
)
end

it "caches with the cache_key_prefix with the non-nil value" do
result = Thing.find(1)
expect(read_from_cache("thing/1")).to eq(result)
Thing.instance_variable_set(:@cache_key_prefix, nil) # Remove memoization

# Should now be memoized
result = Thing.find(1, reload: true)
expect(read_from_cache("prefix123/thing/1")).to eq(result)
result = Thing.find(1, reload: true)
expect(read_from_cache("prefix123/thing/1")).to eq(result)

# Removing memoization will reset it
Thing.instance_variable_set(:@cache_key_prefix, nil) # Remove memoization
result = Thing.find(1, reload: true)
expect(read_from_cache("prefix456/thing/1")).to eq(result)
end
end
end
end

Expand Down

0 comments on commit 170ae8e

Please sign in to comment.