0.1.2-beta.6 • Published 8 years ago

express-mock-backend v0.1.2-beta.6

Weekly downloads
1
License
ISC
Repository
github
Last release
8 years ago

express-mock-backend

Launching the Mock server

var MockBackend = require("express-mock-rest-api").MockBackend;
var server = new MockBackend(8999,"/mock-api",{});
server.start();

Writing a integration test for your application

In this example I'm using the utils provided in "mock-response-handler". It's a http client calling the mock server rest-api.

import {MockBackendApi} from "mock-response-handler";
import * as axios from "axios";
import {expect} from "chai";

To be able to use the utils provided in the mock-response-handler we need to create an HttpHandler The HttpHandler need to implement two methods: doGet(url,headers) & doPost(url,entity,headers) In this example I'm using axios http library

class ExampleHttpClient {
    /**
     *
     * @param url - full url to the rest endpoint
     * @param headers
     * @returns {Promise} promise resolving the json from the response
     */
    doGet(url,headers) {
        return this._toJSON(axios.get(url,headers ? { headers : headers } : null))
    }

    /**
     *
     * @param url
     * @param entity
     * @param headers
     * @returns {*}
     */
    doPost(url,entity, headers) {
        return this._toJSON(axios.post(url,entity, headers ? { headers : headers } : null));
    }
    _toJSON(promise) {
        return new Promise((resolve,reject)=>{
            promise.then(response => {
                resolve(response.data);
            }).catch( err => {
                reject(err);
            })
        });
    }
}

Sample service, what you eventually will test in your real code

class SampleService {
    fetchUsers() {
        return new Promise((resolve,reject)=>{
            axios.get("http://localhost:8999/mock-api/users").then( response => {
               resolve(response.data);
            }).catch( err => {
                reject(err);
            });
        })
    }
}

Mocha test using chai

/**
 * Mocha test using chai
 */
describe("Sample test using express mock rest api",()=>{

    var mockServerApi;
    var sampleService = new SampleService();
    beforeEach(()=>{
        mockServerApi = new MockBackendApi(new ExampleHttpClient(), "http://localhost:8999","/responses", "/mock-api");
        var mockResponse = mockServerApi.mockResponse();

        //the response I want from my mocked rest api
        var mockedResponse = [
            { username : "Pete"},
            { username : "Fred "}
        ];

        //tell the mock server to return the mocked response for the path /users
        return mockResponse.path("/users")
            .status(200)
            .method("GET")
            .body(mockedResponse)
            .execute();

    });
    afterEach(()=>{
        //reset the mock server
        return mockServerApi.reset();
    });

    it("Test sample request to mocked endpoint", ()=>{
        return sampleService.fetchUsers().then( users => {
            expect(users.length).to.equal(2);
            expect(users[0].username).to.equal("Pete");
        });
    });

    it("Example on how to verify the request", ()=>{
        return sampleService.fetchUsers().then( users => {
            expect(users.length).to.equal(2);
            expect(users[0].username).to.equal("Pete");
        }).then(() => {
            //verify the request your application (SampleService) sent
            return mockServerApi.getResponses("/users","GET").then( responses => {
                //all the responses that you have mocked
                expect(responses.length).to.equal(1);
                expect(responses[0].responseHttpStatus).to.equal(200);

                //all requests that have been mapped to this response
                expect(responses[0].requests.length).to.equal(1);
                expect(responses[0].requests[0].method).to.equal("GET");
            });
        }).then(() => {
            //since we only have one mocked response this will have the same responses as previous .then
            return mockServerApi.getResponses().then( responses => {
                expect(responses.length).to.equal(1);
            });
        });;
    });

});