This post describes how to use Mountebank imposters as Mock Objects/Test Spies via HTTP with F#.
As a learning exercise, for mountebank, but also for F#, I wrote a few F# functions to communicate with Mountebank imposters via HTTP and a few passing tests. The rest of the post describes what I learned so far.
What is Mountebank?
Mountebank is a tool (and currently the only one) which provides multi-protocol, multi-language, on-demand, Test Doubles over the wire, named imposters.
Now we can send a mail message via SMTP to the Mountebank imposter using port 4547:
In F# this can be written as:
To get the captured requests from the imposter we can issue a GET or a DELETE request. Normally, this happens during the fixture teardown phase via DELETE:
According to Hypertext Transfer Protocol -- HTTP/1.1 for HTTP method definitions, it is OK to issue a DELETE to also get the captured requests from the imposter: *"A successful response SHOULD be 200 (OK) if the response includes an entity describing the status" -- Hypertext Transfer Protocol -- HTTP/1.1, 9.7 DELETE*.
The response is:
In F# the requests JSON property can be decomposed and extracted using the FSharp.Data JSON Parser and Http modules as:
The signature of GetCapturedRequests function is:
The value of the subject property can be similarly decomposed and extracted with Pattern Matching:
The Mountebank website uses the Mock Object terminology when verifying indirect outputs. However, the examples shown here don't setup expectations - instead they only verify *captured*-indirect outputs of the SUT; thus the Test Spy terminology is used in code.
Scenario: SMTP client transmits correct number of requests.
In this case, it’s only necessary to verify that the SMTP request on the imposter was made only once:
The complete source code is available on this gist - any comments or suggestions are always welcome.