Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ Public error classes:
- `FixtureKit::FixtureDefinitionNotFound`
- `FixtureKit::RunnerAlreadyStartedError`
- `FixtureKit::CircularFixtureInheritance`
- `FixtureKit::MissingCoderDataError`

## Requirements

Expand Down
1 change: 1 addition & 0 deletions lib/fixture_kit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CacheMissingError < Error; end
class FixtureDefinitionNotFound < Error; end
class RunnerAlreadyStartedError < Error; end
class CircularFixtureInheritance < Error; end
class MissingCoderDataError < Error; end

autoload :VERSION, File.expand_path("fixture_kit/version", __dir__)
autoload :Configuration, File.expand_path("fixture_kit/configuration", __dir__)
Expand Down
8 changes: 7 additions & 1 deletion lib/fixture_kit/memory_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ def initialize(data:, exposed:)
end

def data_for(coder_class)
data.fetch(coder_class)
data.fetch(coder_class) do
raise MissingCoderDataError,
"The fixture cache has no data for #{coder_class}. This usually means " \
"the cache was written before #{coder_class} was registered as a coder. " \
"Regenerate the fixture cache so it includes this coder (clear the cache " \
"directory, or run without FIXTURE_KIT_PRESERVE_CACHE=1)."
end
end
end
end
24 changes: 24 additions & 0 deletions spec/unit/memory_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,28 @@
expect(cache).to be_frozen
end
end

describe "#data_for" do
it "returns the data stored for a coder class" do
cache = described_class.new(
data: { FixtureKit::ActiveRecordCoder => { "User" => "SQL" } },
exposed: {}
)

expect(cache.data_for(FixtureKit::ActiveRecordCoder)).to eq({ "User" => "SQL" })
end

it "raises an actionable error when the cache predates a coder" do
# A cache written before this coder was registered has no entry for it.
# Rather than an opaque KeyError, callers should learn the cache is stale
# and how to fix it — naming the coder and pointing to regeneration.
cache = described_class.new(data: {}, exposed: {})

expect { cache.data_for(FixtureKit::ActiveRecordCoder) }
.to raise_error(FixtureKit::MissingCoderDataError) do |error|
expect(error.message).to include("FixtureKit::ActiveRecordCoder")
expect(error.message).to match(/regenerate/i)
end
end
end
end