If you make a web request in a test that isn't registered, WebMock will raise an exception, suggest how you can mock it, and list all the previously registered mocks:
I'd like to be able to dump all the registered mocks, just like the green-circled section above. How can I do that?
I've tried several invocations along these lines, without success:
snippet = WebMock::RequestSignatureSnippet.new(nil)
puts snippet.request_stubs
But I get this error on the first line:
ArgumentError:
wrong number of arguments (given 0, expected 1)
# ./spec/models/user_spec.rb:113:in `new'
While I don't understand the wrong number of argument
error, (I am passing it a nil, afterall) I suspect I'm just barking up the wrong tree and there's a simple way to do this. I haven't found any reference in the docs or searching online.
I could duplicate code and write my own dumper, but that seems silly.
Is there a simple way to get a formatted dump of all the registered webmocks, short of making a bad network request?
You could check the content the request_stub
class variable defined for WebMock::StubRegistry
:
WebMock::StubRegistry.instance.request_stubs
=> [#<WebMock::RequestStub:.., ...]
There you can use every RequestStub
request_pattern
method and their content.
Using the RequestSignatureSnippet
.
What you need for initializing a RequestSignatureSnippet
object is a RequestSignature
object, which itself, expects to receive a HTTP method (as a String) and a URI (as a String as well):
signature = WebMock::RequestSignature.new("GET", "http://example.com")
puts WebMock::RequestSignatureSnippet.new(signature).request_stubs
# "registered request stubs: ..."