JavaScript如何知道执行失败功能?

时间:2016-12-02 15:21:30

标签: javascript node.js jasmine

我正在与现有项目的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)块。

我有两个问题:

  1. done传递给checkResult时,checkResult' s seeIfThereIsError参数中返回的函数如何填充?
    1. 如何创建签名{ [Function] fail: [Function] }
  2. 简而言之,我怎样才能从头开始创建一个任意的例子来理解这些都是如何组合在一起的工作部分(语法)?

1 个答案:

答案 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)是否为您提供的输出取决于浏览器以及可能的其他特定于实现的启发式方法。