我是新手,所以请温柔一点:-)
我有几个单元测试,它们都共享相同的超级代理请求 Get 和 Post。
是否可以提取这些请求,以便我可以只运行一个函数而不是复制粘贴整个请求?
例:
var url = URL_GOES_HERE;
it('responds with totalPrice = 0 when numOfDays = 0', function(done) {
request(url)
.post('/bookRoom')
.send({"numOfDays": 0, "checkInDate": todayDate})
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.end(function(err, res){
var body = res.body;
expect(body.totalPrice).to.eql(0);
done();
});
我想将其提取到一个函数中,该函数将采用参数(例如正在发布的 JSON),并且我能够对其进行断言,如示例中所示 - expect(body.totalPrice).to.eql(0);
当然!将其设置为回调:
var url = URL_GOES_HERE;
function postBookRoom(postBody, cb) {
request(url)
.post('/bookRoom')
.send(postBody)
.set(...etc etc...)
.end(cb); // The callback will be called with parameters err and res
}
it('responds with totalPrice = 0 when numOfDays = 0', function (done) {
postBookRoom({
'numOfDays': 0,
'checkInDate': todayDate
}, function (err, res) {
// This is the function that postBookRoom will call back
expect(res.body.totalPrice).to.eql(0);
done();
});
});