使用Nock和SuperTest模拟API

时间:2017-09-26 15:10:02

标签: request mocha nock

我有一个非常简单的GET测试,通过Mocha运行

import chai from 'chai'
import chaiHttp from 'chai-http'
import {should, expect} from 'chai'
import nock from 'nock'
import request from 'supertest'

    const url  = 'http://mysite-beta.com'
    const api = '/api/user'


    describe('/GET route', () => {
        it('it should GET the mocked response', (done) => {      
            nock(url)
            .get(api)
            .reply(200, {
              "status": 200,
              "message": "This is a mocked response"
            });

          request(url)
            .get(api)
            .end(function (err, res) {
              // ****** the next line gives an error ******
              expect(res.body.status).to.equal(200);
              expect(res.body.message).to.equal("This is a mocked response");
              done();
            });
        });
      });

但是当我运行它时,我得到TypeError: Cannot read property 'body' of undefined。 res未定义 一切都是安装的,例如npm包等

0 个答案:

没有答案