Skip to content

Commit

Permalink
Fix failing tests on Ruby 3.4 because of hash inspect changes
Browse files Browse the repository at this point in the history
Ruby has changed the output of `Hash.inspect`, causing some test failures.

Instead of hardcoding it, just call inspect itself to pass tests on old and new rubies

Ref:
* ruby/ruby#10924
* https://bugs.ruby-lang.org/issues/20433
  • Loading branch information
Earlopain committed Oct 28, 2024
1 parent 9ff63ac commit 6faacb2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion spec/unit/matchers/hash_excluding_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Matchers
end

it 'describes itself properly' do
expect(HashExcludingMatcher.new(a: 1).inspect).to eq 'hash_excluding({"a"=>1})'
expect(HashExcludingMatcher.new(a: 1).inspect).to eq "hash_excluding(#{{"a" => 1}.inspect})"
end

describe 'success' do
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/matchers/hash_including_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Matchers
end

it "describes itself properly" do
expect(HashIncludingMatcher.new(a: 1).inspect).to eq "hash_including({\"a\"=>1})"
expect(HashIncludingMatcher.new(a: 1).inspect).to eq "hash_including(#{{"a"=>1}.inspect})"
end

describe "success" do
Expand Down
15 changes: 9 additions & 6 deletions spec/unit/request_pattern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,25 @@
end

it "should report string describing itself with query params" do
expect(WebMock::RequestPattern.new(:get, /.*example.*/, query: {'a' => ['b', 'c']}).to_s).to eq(
"GET /.*example.*/ with query params {\"a\"=>[\"b\", \"c\"]}"
query = {'a' => ['b', 'c']}
expect(WebMock::RequestPattern.new(:get, /.*example.*/, query: query).to_s).to eq(
"GET /.*example.*/ with query params #{query.inspect}"
)
end

it "should report string describing itself with query params as hash including matcher" do
query = {'a' => ['b', 'c']}
expect(WebMock::RequestPattern.new(:get, /.*example.*/,
query: WebMock::Matchers::HashIncludingMatcher.new({'a' => ['b', 'c']})).to_s).to eq(
"GET /.*example.*/ with query params hash_including({\"a\"=>[\"b\", \"c\"]})"
query: WebMock::Matchers::HashIncludingMatcher.new(query)).to_s).to eq(
"GET /.*example.*/ with query params hash_including(#{query.inspect})"
)
end

it "should report string describing itself with body as hash including matcher" do
query = {'a' => ['b', 'c']}
expect(WebMock::RequestPattern.new(:get, /.*example.*/,
body: WebMock::Matchers::HashIncludingMatcher.new({'a' => ['b', 'c']})).to_s).to eq(
"GET /.*example.*/ with body hash_including({\"a\"=>[\"b\", \"c\"]})"
body: WebMock::Matchers::HashIncludingMatcher.new(query)).to_s).to eq(
"GET /.*example.*/ with body hash_including(#{query.inspect})"
)
end
end
Expand Down
7 changes: 4 additions & 3 deletions spec/unit/request_signature_snippet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@
end

describe "#request_stubs" do
let(:body) { {"a" => "b"} }
before :each do
WebMock.stub_request(:get, "https://www.example.com").with(body: {"a" => "b"})
WebMock.stub_request(:get, "https://www.example.com").with(body: body)
end

context "when showing the body diff is turned off" do
Expand All @@ -60,7 +61,7 @@
result = subject.request_stubs
result.sub!("registered request stubs:\n\n", "")
expect(result).to eq(
"stub_request(:get, \"https://www.example.com/\").\n with(\n body: {\"a\"=>\"b\"})"
"stub_request(:get, \"https://www.example.com/\").\n with(\n body: #{body.inspect})"
)
end

Expand All @@ -74,7 +75,7 @@
result = subject.request_stubs
result.sub!("registered request stubs:\n\n", "")
expect(result).to eq(
"stub_request(:get, \"https://www.example.com/\").\n with(\n body: {\"a\"=>\"b\"})\n\nBody diff:\n [[\"-\", \"key\", \"different value\"], [\"+\", \"a\", \"b\"]]\n"
"stub_request(:get, \"https://www.example.com/\").\n with(\n body: #{body.inspect})\n\nBody diff:\n [[\"-\", \"key\", \"different value\"], [\"+\", \"a\", \"b\"]]\n"
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/stub_request_snippet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
expected = <<-STUB
stub_request(:post, "http://www.example.com/").
with(
body: {"user"=>{"first_name"=>"Bartosz"}},
body: #{{"user"=>{"first_name"=>"Bartosz"}}.inspect},
headers: {
\t 'Content-Type'=>'application/x-www-form-urlencoded'
}).
Expand Down

0 comments on commit 6faacb2

Please sign in to comment.