我是新手,所以请保持温柔: - )
我有几个单元测试,它们都对Get和Post共享相同的superagent请求。
是否可以提取这些请求,以便我可以运行一个函数而不是复制粘贴整个请求?
示例:
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);
答案 0 :(得分: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();
});
});