Files
ringe edc0b2945b
CI / test (3.1) (push) Successful in 35s
CI / test (3.2) (push) Successful in 33s
CI / test (3.3) (push) Successful in 33s
tolerate empty string result from Fiken API
2026-06-23 14:52:51 +02:00

54 lines
1.4 KiB
Ruby

# frozen_string_literal: true
RSpec.describe Fiken::Object do
subject(:object) do
described_class.new(
"invoiceNumber" => 5,
"customer" => { "name" => "Acme" },
"lines" => [{ "vat" => 25 }, { "vat" => 0 }]
)
end
it "exposes camelCase keys via dot access" do
expect(object.invoiceNumber).to eq(5)
end
it "exposes the same keys via snake_case dot access" do
expect(object.invoice_number).to eq(5)
end
it "supports hash-style access with string or symbol keys" do
expect(object[:invoiceNumber]).to eq(5)
expect(object["invoiceNumber"]).to eq(5)
end
it "wraps nested hashes recursively" do
expect(object.customer.name).to eq("Acme")
end
it "wraps arrays of hashes recursively" do
expect(object.lines.map(&:vat)).to eq([25, 0])
end
it "round-trips to a plain hash" do
expect(object.to_h).to eq(
invoiceNumber: 5,
customer: { name: "Acme" },
lines: [{ vat: 25 }, { vat: 0 }]
)
end
it "raises NoMethodError for unknown keys" do
expect { object.nope }.to raise_error(NoMethodError)
end
it "answers key? for present and absent keys" do
expect(object.key?(:invoiceNumber)).to be(true)
expect(object.key?(:missing)).to be(false)
end
it "treats a non-Hash body (e.g. empty string from a 204 response) as empty" do
expect(described_class.new("").keys).to eq([])
end
end