我正在与现有项目的jasmine和Supertest合作。
let checkResult = require('./check-result');
it('should do something', function(done){
request
.post('/route')
.expect(results => {
expect(results).toBeTruthy();
})
.end(checkResult(done));
});
当我console.log(done)
时,我得到以下输出:{ [Function] fail: [Function] }
以下是我们的checkResult模块。
//check-result
module.exports = function checkResult(done){
return function(seeIfThereIsError){
if(seeIfThereIsError){
done.fail(seeIfThereIsError)
} else {
done()
}
}
};
发生错误时,执行if(seeIfThereIsError)
块。
我有两个问题:
done
传递给checkResult
时,checkResult
' s seeIfThereIsError
参数中返回的函数如何填充?
{ [Function] fail: [Function] }
?简而言之,我怎样才能从头开始创建一个任意的例子来理解这些都是如何组合在一起的工作部分(语法)?
答案 0 :(得分:1)
将
done
传递给checkResult
时,checkResult
的{{1}}参数中返回的函数如何填充?
该函数传递给seeIfThereIsError
并由end
调用。即end
会将值传递给函数。最简单的形式如下:
request.end = function(callback) {
callback(false);
};
如何创建签名
{ [Function] fail: [Function] }
?
console.log
如何表示功能未标准化。此输出只是告诉您该值是一个具有自定义属性fail
的函数对象,它也是一个函数。
如果您想自己创建这样的值,可以这样做:
function done() {}
done.fail = function() {};
console.log(done)
是否为您提供的输出取决于浏览器以及可能的其他特定于实现的启发式方法。