Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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);
- },
- );
- });
- 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);
- },
- );
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement