Mocks
Mocks are a type
of test double that allow you to verify a call was made without relying on the server
response. mountebank enables mocking through the requests
element on
an imposter. The fields saved for each request depends on the protocol, and are documented
in the protocol pages linked to from the sidebar. It is up to the client code to
implement the mock verification using the requests
array.
Example
The SMTP protocol is one where mocking is very valuable, since most applications don't rely on complex SMTP responses, but it is useful to verify that the application sent an SMTP request without actually sending an email.
First let's create an imposter:
POST /imposters HTTP/1.1
Host: localhost:35553
Accept: application/json
Content-Type: application/json
{
"port": 4545,
"protocol": "smtp",
"recordRequests": true
}
Now make the following SMTP request in your application:
From: "Customer Service" <customer-service@company.com>
To: "Customer" <customer@domain.com>
Subject: Thank you for your order
Hello Customer,
Thank you for your order from company.com. Your order will
be shipped shortly.
Your friendly customer service department.
You can verify the call by making either a GET
or
DELETE
call to the imposter:
GET /imposters/4545 HTTP/1.1
Host: localhost:35553
Accept: application/json
HTTP/1.1 200 OK
Vary: Accept
Content-Type: application/json; charset=utf-8
Content-Length: 1116
Date: Thu, 09 Jan 2014 02:30:31 GMT
Connection: keep-alive
{
"protocol": "smtp",
"port": 4545,
"numberOfRequests": 1,
"recordRequests": true,
"requests": [
{
"requestFrom": "127.0.0.1 ",
"ip": "127.0.0.1 ",
"envelopeFrom": "customer-service@company.com",
"envelopeTo": ["customer@domain.com"],
"from": {
"address": "customer-service@company.com",
"name": "Customer Service"
},
"to": [
{
"address": "customer@domain.com",
"name": "Customer"
}
],
"cc": [],
"bcc": [],
"subject": "Thank you for your order",
"priority": "normal",
"references": [],
"inReplyTo": [],
"text": "Hello Customer,\nThank you for your order from company.com. Your order will\nbe shipped shortly.\n\nYour friendly customer service department.",
"html": "",
"attachments": [],
"timestamp": "2015-01-09T02:30:31.850Z "
}
],
"stubs": [],
"_links": {
"self": { "href": "http://localhost:35553/imposters/4545" },
"stubs": { "href": "http://localhost:35553/imposters/4545/stubs" }
}
}
Now you can use the requests
element in your test code to validate
the application sent the correct information.
DELETE /imposters/4545 HTTP/1.1
Host: localhost:35553
Accept: application/json