Getting Started with Maddox
The Microservices unit testing framework.
A Quick Start Guide To Maddox. See how to test complex scenarios often found in your microservices environment.
// npm install maddox
const Maddox = require("maddox");
const Scenario = Maddox.scenarios.HttpReqScenario;
it("should show a basic HttpReqScenario maddox test.", function (done) {
new Scenario(this)
.mockThisFunction("ProxyMockName", "getFirstName", ProxyMock)
.withEntryPoint(Controller, "read")
.withHttpRequest(httpRequestParams)
.resShouldBeCalledWith("send", expectedResponse)
.resShouldBeCalledWith("status", expectedStatusCode)
.resDoesReturnSelf("status")
.shouldBeCalledWith("ProxyMockName", "getFirstName", getFirstName1Params)
.doesReturnWithPromise("ProxyMockName", "getFirstName", getFirstName1Result)
.perf()
.test(done);
});
it("should show a more complex HttpReqScenario maddox text that describes more functionality.", function (done) {
new Scenario(this) // Create a new Scenario
.mockThisFunction("ProxyMockName", "getFirstName", ProxyMock) // Mock ProxyClass.getFirstName
.mockThisFunction("ProxyMockName", "getMiddleName", ProxyMock) // Mock ProxyClass.getMiddleName
.mockThisFunction("ProxyMockName", "getLastName", ProxyMock) // Mock ProxyClass.getMiddleName
.withEntryPoint(Controller, "read") // Declare Controller.read to be the entry point for the test
.withHttpRequest(httpRequestParams) // Use the object 'httpRequestParams' as the input into the Controller
// NOTE: The HTTP Response Object is created by Maddox and passed in automatically.
.resShouldBeCalledWith("send", expectedResponse) // Test that res.send is called with the same parameters that are defined in 'expectedResponse'
.resShouldBeCalledWith("status", expectedStatusCode) // Test that res.status is called with the same parameters that are defined in 'expectedStatusCode'
.resDoesReturnSelf("status") // Allow Express's expected chainable call res.status().send()
.shouldBeCalledWith("ProxyMockName", "getFirstName", getFirstName1Params) // Test that the first call to ProxyClass.getFirstName is called with the same parameters that are defined in 'getFirstName1Params'
.doesReturnWithPromise("ProxyMockName", "getFirstName", getFirstName1Result) // When ProxyClass.getFirstName is called for the first time, return 'getFirstName1Result' using Promise A+ protocol
.shouldBeCalledWith("ProxyMockName", "getFirstName", getFirstName2Params) // Test that the second call to ProxyClass.getFirstName is called with the same parameters that are defined in 'getFirstName2Params'
.doesReturnWithPromise("MockName", "getFirstName", getFirstName2Result) // When ProxyClass.getFirstName is called for the second time, return 'getFirstName2Result' using Promise A+ protocol
.shouldBeCalledWith("ProxyMockName", "getMiddleName", getMiddleNameParams) // Test that the first call to ProxyClass.getMiddleName is called with the same parameters that are defined in 'getMiddleNameParams'
.doesReturn("MockName", "getMiddleName", getMiddleNameResult) // When ProxyClass.getMiddleName is called for the first time, return 'getMiddleNameResult' synchronously
.shouldBeCalledWith("ProxyMockName", "getLastName", getLastNameParams) // Test that the first call to ProxyClass.getLastName is called with the same parameters that are defined in 'getLastNameParams'
.doesReturnWithCallback("ProxyMockName", "getLastName", getLastNameResult) // When ProxyClass.getLastName is called for the first time, return 'getLastNameResult' using the callback paradigm. i.e. callback(err, result)
.perf() // You can now run this test as a performance test with the maddox cli.
.test(done); // Executes the test. Up to this point, we have only build out the test context. No tests are executed until the test function is called.
// NOTE: All scenarios are asynchronous. Ensure that that 'done' function is passed in or executed by you.
});
Updated over 7 years ago