mountebank

mountebank - over the wire test doubles

Fork me on GitHub

the apothecary

Mocks

Mocks are a type of test double that allow you to verify a call was made without relying on the server response. mountebank supports 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.

Note, the --mock command line flag must be set to enable mock verification. Mocking requires mountebank to remember information about each request to each imposter. For long-running instances of mountebank, this represents a memory leak, so this functionality is turned off by default.

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:42963
Accept: application/json
Content-Type: application/json

{
  "port": 4545,
  "protocol": "smtp"
}

HTTP/1.1 201 Created
Access-Control-Allow-Origin: *
Location: http://localhost:42963/imposters/4545
Content-Type: application/json; charset=utf-8
Content-Length: 190
Date: Sun, 05 Jan 2014 16:28:40 GMT
Connection: keep-alive

{
  "protocol": "smtp",
  "port": 4545,
  "numberOfRequests": 0,
  "requests": [],
  "stubs": [],
  "_links": {
    "self": {
      "href": "http://localhost:42963/imposters/4545"
    }
  }
}

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:42963
Accept: application/json

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Vary: Accept
Content-Type: application/json; charset=utf-8
Content-Length: 976
Date: Thu, 09 Jan 2014 02:30:31 GMT
Connection: keep-alive

{
  "protocol": "smtp",
  "port": 4545,
  "numberOfRequests": 1,
  "requests": [
    {
      "requestFrom": "::ffff: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:42963/imposters/4545"
    }
  }
}

Now you can use the requests element in your test code to validate the application sent the correct information.