Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 使用mocha测试多个http请求_Unit Testing_Mocha.js_Supertest_Nock_Supertest As Promised - Fatal编程技术网

Unit testing 使用mocha测试多个http请求

Unit testing 使用mocha测试多个http请求,unit-testing,mocha.js,supertest,nock,supertest-as-promised,Unit Testing,Mocha.js,Supertest,Nock,Supertest As Promised,我已经努力解决这个问题好几天了; 使用mocha为本案例创建测试: app.post('/approval', function(req, response){ request.post('https://git.ecommchannel.com/api/v4/users/' + req.body.content.id + '/' + req.body.content.state + '?private_token=blabla', function (error, resp, body) {

我已经努力解决这个问题好几天了; 使用mocha为本案例创建测试:

app.post('/approval', function(req, response){
request.post('https://git.ecommchannel.com/api/v4/users/' + req.body.content.id + '/' + req.body.content.state + '?private_token=blabla', function (error, resp, body) {
    if (resp.statusCode == 201) {
                //do something
            } else {
                response.send("failed"), response.end();
            }
        });  
    } else {
        response.send("failed"), response.end();
    }
});
}))

我尝试了几种方法,使用supertest测试'/approval',使用nock测试对GitAPI的post请求。但它总是把“状态码”变成未定义的。我认为这是因为index.js中对git api的请求不在某个函数中(?) 所以我不能实现这样的东西: 或

})

然后我试着用supertest作为承诺

    it('approve-block-using-promise', () => {
       return promise(_import.app)
        .post('/approval')
        .send(req = {
            content: {
                id: 1,
                state: 'yes'
            },
            _id: 1
        })
        .expect(200)
        .then(function(res){
            return promise(_import.app)
            .post("https://git.ecommchannel.com/api/v4/users/")
            .send('1/yes', 'private_token=blabla')
            .expect(201);
        })
})

但它给出了错误:ECONNEREFUSED:连接被拒绝。我没有找到任何解决错误的方法。一些消息来源说,这需要完成。。但它给出了另一条错误消息,“确保调用“>”了“done()”。您发布的代码有一些错误,我将尝试列出它们,但我也在下面提供了一个完整的传递示例

首先,您在控制器中调用
git.ecommchannel
,这是一篇没有正文的帖子。虽然这不会导致您看到的错误,从技术上讲也没有错误,但这很奇怪。因此,您应该仔细检查应该发送的数据是什么

接下来,我假设在创建问题时这是一个复制/粘贴问题,但是控制器中的请求回调无效。括号不匹配,发送“失败”出现两次

您的Nock设置有两个问题。第一个问题是
Nock
的参数应该只有原点,没有路径。因此
/api/v4/users
必须移动到
post
方法的第一个参数中。另一个问题是第二个参数传递到
post
,这是post主体的可选匹配。如如上所述,您当前没有发送正文,因此Nock将始终无法匹配和替换该请求。在下面的示例中,
private_令牌
已被移动以与请求的查询字符串匹配,如图所示

调用
nockingGit
太晚了。在使用Supertest调用您的Express应用程序之前,Nock需要注册mock。您已经在
end
方法中调用了它,到那时已经太晚了

标记为
approve block using promise
的测试在第二次调用应用程序时出现问题。它在Express应用程序上通过Supertest调用
post
,但是,该
post
方法的第一个参数是您对应用程序发出的请求路径。它与调用
git.ecommchannel无关。因此,在这种情况下,您的Express应用程序应该返回404 Not Found

const express = require('express')
const nock = require('nock')
const request = require('request')
const supertest = require('supertest')

const app = express()
app.use(express.json())

app.post('/approval', function(req, response) {
  const url = 'https://git.ecommchannel.com/api/v4/users/' + req.body.content.id + '/' + req.body.content.state
  request.post({
      url,
      qs: {private_token: 'blabla'}
      // body: {} // no body?
    },
    function(error, resp, body) {
      if (error) {
        response.status(500).json({message: error.message})
      } else if (resp.statusCode === 201) {
        response.status(200).send("OK")
      } else {
        response.status(500).send("failed").end();
      }
    });
});

const nockingGit = () => {
  nock('https://git.ecommchannel.com')
    .post('/api/v4/users/1/yes')
    .query({private_token: 'blabla'})
    .reply(201, {"data": "hello world"});
};

it('approval', (done) => {
  const reqPayload = {
    content: {
      id: 1,
      state: 'yes'
    },
    _id: 1
  }

  nockingGit();

  supertest(app)
    .post('/approval')
    .send(reqPayload)
    .expect(200)
    .expect('Content-Type', /html/)
    .end(function(err) {
      done(err);
    })
})

你能更新你的问题以包含测试的
nock
部分吗?另外,在你的第一个测试示例中,你向你的本地应用程序发出了两个请求。对你的应用程序的第一个或第二个请求是错误的吗?嗨,Matt,我已经将nock部分包括在问题中。我使用supertest作为承诺的测试在第二个请求中给出了错误。嗨,马特:谢谢你的回复。我真的很感激。现在效果很好:)但我现在面临另一个问题……还是关于诺克。你能看看我的问题吗>
    it('should respond to only certain methods', function(done) {
    async.series([
        function(cb) { request(_import.app).post('/approval')
        .send(req = {
            content: {
                id: 1,
                state: 'yes'
            },
            _id: 1
        })
        .expect(200, cb); },
        function(cb) { request(_import.app).post('/https://git.ecommchannel.com/api/v4/users/').send('1/yes', 'private_token=blabla').expect(201, cb); },
    ], done);
});
const express = require('express')
const nock = require('nock')
const request = require('request')
const supertest = require('supertest')

const app = express()
app.use(express.json())

app.post('/approval', function(req, response) {
  const url = 'https://git.ecommchannel.com/api/v4/users/' + req.body.content.id + '/' + req.body.content.state
  request.post({
      url,
      qs: {private_token: 'blabla'}
      // body: {} // no body?
    },
    function(error, resp, body) {
      if (error) {
        response.status(500).json({message: error.message})
      } else if (resp.statusCode === 201) {
        response.status(200).send("OK")
      } else {
        response.status(500).send("failed").end();
      }
    });
});

const nockingGit = () => {
  nock('https://git.ecommchannel.com')
    .post('/api/v4/users/1/yes')
    .query({private_token: 'blabla'})
    .reply(201, {"data": "hello world"});
};

it('approval', (done) => {
  const reqPayload = {
    content: {
      id: 1,
      state: 'yes'
    },
    _id: 1
  }

  nockingGit();

  supertest(app)
    .post('/approval')
    .send(reqPayload)
    .expect(200)
    .expect('Content-Type', /html/)
    .end(function(err) {
      done(err);
    })
})