Skip to content

Instantly share code, notes, and snippets.

@nardhar
Last active September 13, 2017 20:07
Show Gist options
  • Save nardhar/845c1da9d005aee01dd0301c8e7d78b7 to your computer and use it in GitHub Desktop.
Save nardhar/845c1da9d005aee01dd0301c8e7d78b7 to your computer and use it in GitHub Desktop.

Revisions

  1. nardhar revised this gist Sep 13, 2017. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions shouldjs-plugin-idea.js
    Original file line number Diff line number Diff line change
    @@ -39,4 +39,21 @@ describe('Starting controller testing', () => {
    },
    );
    });

    it('PUT a record with query and body', () => {
    requestTest.put(
    '1',
    // body of the request
    {
    number: 123,
    name: 'my sample name',
    },
    // response callback
    (res) => {
    should(res.status).be.eql(200);
    should(res.body).have.property('name', 'my sample name');
    should(res.body).have.property('number', 123);
    },
    );
    });
    });
  2. nardhar created this gist Sep 13, 2017.
    42 changes: 42 additions & 0 deletions shouldjs-plugin-idea.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    // Is there something like this?
    const RequestTest = require('should-request-test');

    describe('Starting controller testing', () => {
    // calling the express app
    const server = require('src/index');
    // controller endpoint
    const endpoint = '/api/v1/myResource';

    const requestTest;

    before((done) => {
    const token = getToken();
    // send the same header on all petitions
    requestTest = new RequestTest({
    server,
    endpoint,
    headers: {
    Authorization: `Bearer ${token}`,
    'Content-Type': 'application/json',
    },
    });
    });

    // all set up and now I just need to send body and test for the response
    // done() could be called after the it callback, like a wrapper
    it('POST a record', () => {
    requestTest.post(
    // body of the request
    {
    number: 123,
    name: 'my sample name',
    },
    // response callback
    (res) => {
    should(res.status).be.eql(201);
    should(res.body).have.property('name', 'my sample name');
    should(res.body).have.property('number', 123);
    },
    );
    });
    });