Refactor modeling spec to use a new "a_record_matching" matcher

This commit is contained in:
Emi Matchu 2024-10-24 14:42:05 -07:00
parent 3b5b13c172
commit a54a844e03
5 changed files with 51 additions and 29 deletions

View file

@ -1,5 +1,6 @@
require 'rails_helper' require 'rails_helper'
require_relative '../mocks/custom_pets' require_relative '../support/mocks/custom_pets'
require_relative '../support/matchers/a_record_matching'
RSpec.describe Pet, type: :model do RSpec.describe Pet, type: :model do
fixtures :colors, :species, :zones fixtures :colors, :species, :zones
@ -30,33 +31,40 @@ RSpec.describe Pet, type: :model do
it "has new assets for the biology layers" do it "has new assets for the biology layers" do
subject.save! # TODO: I wish this were set up before saving. subject.save! # TODO: I wish this were set up before saving.
s = subject.pet_state.swf_assets.sort_by(&:id) expect(subject.pet_state.swf_assets).to contain_exactly(
a_record_matching(
expect(s.size).to eq 4 type: "biology",
expect(s[0].type).to eq "biology" remote_id: 10083,
expect(s[0].remote_id).to eq 10083 zone_id: 37,
expect(s[0].zone_id).to eq 37 url: "https://images.neopets.com/cp/bio/swf/000/000/010/10083_8a1111a13f.swf",
expect(s[0].url).to eq "https://images.neopets.com/cp/bio/swf/000/000/010/10083_8a1111a13f.swf" manifest_url: "https://images.neopets.com/cp/bio/data/000/000/010/10083_8a1111a13f/manifest.json",
expect(s[0].manifest_url).to eq "https://images.neopets.com/cp/bio/data/000/000/010/10083_8a1111a13f/manifest.json" zones_restrict: "0000000000000000000000000000000000000000000000000000",
expect(s[0].zones_restrict).to eq "0000000000000000000000000000000000000000000000000000" ),
expect(s[1].type).to eq "biology" a_record_matching(
expect(s[1].remote_id).to eq 11613 type: "biology",
expect(s[1].zone_id).to eq 15 remote_id: 11613,
expect(s[1].url).to eq "https://images.neopets.com/cp/bio/swf/000/000/011/11613_f7d8d377ab.swf" zone_id: 15,
expect(s[1].manifest_url).to eq "https://images.neopets.com/cp/bio/data/000/000/011/11613_f7d8d377ab/manifest.json" url: "https://images.neopets.com/cp/bio/swf/000/000/011/11613_f7d8d377ab.swf",
expect(s[1].zones_restrict).to eq "0000000000000000000000000000000000000000000000000000" manifest_url: "https://images.neopets.com/cp/bio/data/000/000/011/11613_f7d8d377ab/manifest.json",
expect(s[2].type).to eq "biology" zones_restrict: "0000000000000000000000000000000000000000000000000000",
expect(s[2].remote_id).to eq 14187 ),
expect(s[2].zone_id).to eq 34 a_record_matching(
expect(s[2].url).to eq "https://images.neopets.com/cp/bio/swf/000/000/014/14187_0e65c2082f.swf" type: "biology",
expect(s[2].manifest_url).to eq "https://images.neopets.com/cp/bio/data/000/000/014/14187_0e65c2082f/manifest.json" remote_id: 14187,
expect(s[2].zones_restrict).to eq "0000000000000000000000000000000000000000000000000000" zone_id: 34,
expect(s[3].type).to eq "biology" url: "https://images.neopets.com/cp/bio/swf/000/000/014/14187_0e65c2082f.swf",
expect(s[3].remote_id).to eq 14189 manifest_url: "https://images.neopets.com/cp/bio/data/000/000/014/14187_0e65c2082f/manifest.json",
expect(s[3].zone_id).to eq 33 zones_restrict: "0000000000000000000000000000000000000000000000000000",
expect(s[3].url).to eq "https://images.neopets.com/cp/bio/swf/000/000/014/14189_102e4991e9.swf" ),
expect(s[3].manifest_url).to eq "https://images.neopets.com/cp/bio/data/000/000/014/14189_102e4991e9/manifest.json" a_record_matching(
expect(s[3].zones_restrict).to eq "0000000000000000000000000000000000000000000000000000" type: "biology",
remote_id: 14189,
zone_id: 33,
url: "https://images.neopets.com/cp/bio/swf/000/000/014/14189_102e4991e9.swf",
manifest_url: "https://images.neopets.com/cp/bio/data/000/000/014/14189_102e4991e9/manifest.json",
zones_restrict: "0000000000000000000000000000000000000000000000000000",
)
)
end end
end end
end end

View file

@ -11,6 +11,11 @@ RSpec.configure do |config|
# ...rather than: # ...rather than:
# # => "be bigger than 2" # # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true expectations.include_chain_clauses_in_custom_matcher_descriptions = true
# Some of our SWF assets are long, and we want all of it when diffing!
# Here, I picked a large number, to approximate "any length" but not
# actually allow infinity.
expectations.max_formatted_output_length = 3000
end end
# rspec-mocks config goes here. You can use an alternate test double # rspec-mocks config goes here. You can use an alternate test double

View file

@ -0,0 +1,7 @@
RSpec::Matchers.define :a_record_matching do |expected|
match do |actual|
expected.all? do |attr_name, expected_value|
actual.read_attribute(attr_name) == expected_value
end
end
end

View file

@ -1,7 +1,9 @@
# We replace Neopets::CustomPets methods with a mocked implementation. # We replace Neopets::CustomPets methods with a mocked implementation.
module Neopets::CustomPets module Neopets::CustomPets
DATA_DIR = Pathname.new(__dir__) / "custom_pets"
def self.fetch_viewer_data(pet_name, ...) def self.fetch_viewer_data(pet_name, ...)
File.open(Rails.root / "spec/mocks/custom_pets/#{pet_name}.json") do |file| File.open(DATA_DIR / "#{pet_name}.json") do |file|
HashWithIndifferentAccess.new JSON.load(file) HashWithIndifferentAccess.new JSON.load(file)
end end
end end