diff --git a/spec/models/pet_spec.rb b/spec/models/pet_spec.rb index 3b361565..241ef69e 100644 --- a/spec/models/pet_spec.rb +++ b/spec/models/pet_spec.rb @@ -1,5 +1,6 @@ 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 fixtures :colors, :species, :zones @@ -30,33 +31,40 @@ RSpec.describe Pet, type: :model do it "has new assets for the biology layers" do subject.save! # TODO: I wish this were set up before saving. - s = subject.pet_state.swf_assets.sort_by(&:id) - - expect(s.size).to eq 4 - expect(s[0].type).to eq "biology" - expect(s[0].remote_id).to eq 10083 - expect(s[0].zone_id).to eq 37 - expect(s[0].url).to eq "https://images.neopets.com/cp/bio/swf/000/000/010/10083_8a1111a13f.swf" - expect(s[0].manifest_url).to eq "https://images.neopets.com/cp/bio/data/000/000/010/10083_8a1111a13f/manifest.json" - expect(s[0].zones_restrict).to eq "0000000000000000000000000000000000000000000000000000" - expect(s[1].type).to eq "biology" - expect(s[1].remote_id).to eq 11613 - expect(s[1].zone_id).to eq 15 - expect(s[1].url).to eq "https://images.neopets.com/cp/bio/swf/000/000/011/11613_f7d8d377ab.swf" - expect(s[1].manifest_url).to eq "https://images.neopets.com/cp/bio/data/000/000/011/11613_f7d8d377ab/manifest.json" - expect(s[1].zones_restrict).to eq "0000000000000000000000000000000000000000000000000000" - expect(s[2].type).to eq "biology" - expect(s[2].remote_id).to eq 14187 - expect(s[2].zone_id).to eq 34 - expect(s[2].url).to eq "https://images.neopets.com/cp/bio/swf/000/000/014/14187_0e65c2082f.swf" - expect(s[2].manifest_url).to eq "https://images.neopets.com/cp/bio/data/000/000/014/14187_0e65c2082f/manifest.json" - expect(s[2].zones_restrict).to eq "0000000000000000000000000000000000000000000000000000" - expect(s[3].type).to eq "biology" - expect(s[3].remote_id).to eq 14189 - expect(s[3].zone_id).to eq 33 - 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" - expect(s[3].zones_restrict).to eq "0000000000000000000000000000000000000000000000000000" + expect(subject.pet_state.swf_assets).to contain_exactly( + a_record_matching( + type: "biology", + remote_id: 10083, + zone_id: 37, + url: "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", + zones_restrict: "0000000000000000000000000000000000000000000000000000", + ), + a_record_matching( + type: "biology", + remote_id: 11613, + zone_id: 15, + url: "https://images.neopets.com/cp/bio/swf/000/000/011/11613_f7d8d377ab.swf", + manifest_url: "https://images.neopets.com/cp/bio/data/000/000/011/11613_f7d8d377ab/manifest.json", + zones_restrict: "0000000000000000000000000000000000000000000000000000", + ), + a_record_matching( + type: "biology", + remote_id: 14187, + zone_id: 34, + url: "https://images.neopets.com/cp/bio/swf/000/000/014/14187_0e65c2082f.swf", + manifest_url: "https://images.neopets.com/cp/bio/data/000/000/014/14187_0e65c2082f/manifest.json", + zones_restrict: "0000000000000000000000000000000000000000000000000000", + ), + a_record_matching( + 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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e5dec724..424c4fca 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,6 +11,11 @@ RSpec.configure do |config| # ...rather than: # # => "be bigger than 2" 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 # rspec-mocks config goes here. You can use an alternate test double diff --git a/spec/support/matchers/a_record_matching.rb b/spec/support/matchers/a_record_matching.rb new file mode 100644 index 00000000..76475629 --- /dev/null +++ b/spec/support/matchers/a_record_matching.rb @@ -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 diff --git a/spec/mocks/custom_pets.rb b/spec/support/mocks/custom_pets.rb similarity index 78% rename from spec/mocks/custom_pets.rb rename to spec/support/mocks/custom_pets.rb index 2dbabc45..33bb3e70 100644 --- a/spec/mocks/custom_pets.rb +++ b/spec/support/mocks/custom_pets.rb @@ -1,7 +1,9 @@ # We replace Neopets::CustomPets methods with a mocked implementation. module Neopets::CustomPets + DATA_DIR = Pathname.new(__dir__) / "custom_pets" + 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) end end diff --git a/spec/mocks/custom_pets/thyassa.json b/spec/support/mocks/custom_pets/thyassa.json similarity index 100% rename from spec/mocks/custom_pets/thyassa.json rename to spec/support/mocks/custom_pets/thyassa.json