webmock icon indicating copy to clipboard operation
webmock copied to clipboard

"You can stub this request with" formats badly within minitest assert_raises

Open Nakilon opened this issue 4 years ago • 1 comments

Trying the "webmock/minitest". Here is the test code:

        assert_raises DirectLink::ErrorNotFound do
          DirectLink.imgur "https://imgur.com/a/wwwwwww"
        end

As I remember the "You can stub this request with ..." has to be formatted well so I could copypaste it. But probably due to the assert_raise block it does not format well:

  1) Failure:
DirectLink::./lib::imgur#test_0004_ErrorNotFound when album is empty [unit.test.rb:252]:
[DirectLink::ErrorNotFound] exception expected, not
Class: <WebMock::NetConnectNotAllowedError>
Message: <"Real HTTP connections are disabled. Unregistered request: HEAD https://api.imgur.com/3/album/wwwwwww/0.json with headers {'Accept'=>'*/*', 'Authorization'=>'Client-ID 00000', 'Host'=>'api.imgur.com', 'User-Agent'=>'Ruby'}\n\nYou can stub this request with the following snippet:\n\nstub_request(:head, \"https://api.imgur.com/3/album/wwwwwww/0.json\").\n  with(\n    headers: {\n\t  'Accept'=>'*/*',\n\t  'Authorization'=>'Client-ID 00000',\n\t  'Host'=>'api.imgur.com',\n\t  'User-Agent'=>'Ruby'\n    }).\n  to_return(status: 200, body: \"\", headers: {})\n\n============================================================">
---Backtrace---
/Users/nakilon/.rbenv/versions/2.3.8/lib/ruby/gems/2.3.0/gems/webmock-3.11.0/lib/webmock/http_lib_adapters/net_http.rb:114:in `request'
...

Or did I forgot something I had to add to make it look good?

Nakilon avatar Dec 28 '20 11:12 Nakilon

@Nakilon the WebMock::NetConnectNotAllowedError exception is not supposed to be caught by assert_raises, but instead caught as a test error and unchanged message printed.

Looking at Minitest, the exceptions ignored by assert_raises are not configurable, https://github.com/seattlerb/minitest/blob/dc6ad179ceac3be92375ac1236f0ee19b9f09df4/lib/minitest/assertions.rb#L406 therefore I don't see any easy way of fixing that, apart from monkey patching assert_raises. Perhaps it's worth opening an issue in Minitest to allow adding exceptions to be ignored by assert_raises.

bblimke avatar Dec 28 '20 16:12 bblimke