Posted about 2 years ago. Visible to the public.

Webmock's hash_including doesn't parse query values to string

Webmocks hash_including is similar to RSpec::Mocks::ArgumentMatchers#hash_including. Be aware that hash_including (webmock v3.0.1) doesn't parse integer values to String.

Without hash including you would say:

Copy
uri = URI('http://example.com/?foo=1&bar=2') stub_request(:get, 'example.com').with(query: {foo: 1, bar: 2}) Net::HTTP.get(uri) # ===> Success

If you only want to check if foo is present you can use hash_including:

Copy
uri = URI('http://example.com/?foo=1&bar=2') stub_request(:get, 'example.com').with(query: hash_including(foo: 1)) Net::HTTP.get(uri) # ===> Error

hash_including doesn't parse query values to string, you need to take care of this:

Copy
uri = URI('http://example.com/?foo=1&bar=2') stub_request(:get, 'example.com').with(query: hash_including(foo: '1')) Net::HTTP.get(uri) # ===> Success

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Avatar
Emanuel De
Last edit:
about 2 years ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Emanuel De to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more