javascript - JavaScript 如何知道执行失败函数?

标签 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] failed: [Function] }

下面是我们的 checkResult 模块。

//check-result
module.exports = function checkResult(done){
  return function(seeIfThereIsError){
    if(seeIfThereIsError){
      done.fail(seeIfThereIsError)
    } else {
      done()
    }
  }
};

当发生错误时,if(seeIfThereIsError) block 将执行。

我有两个问题:

  1. done 传递到 checkResult 时,如何填充 checkResultseeIfThereIsError 参数中返回的函数?
  2. 签名 { [Function] failed: [Function] } 是如何创建的?

简而言之,我如何从头开始创建一个任意示例来理解所有工作部分(语法)如何组合在一起?

最佳答案

When passing done into checkResult how does the returned function in checkResult's seeIfThereIsError argument get populated?

该函数被传递给 end 并由其调用。 IE。 end 将向函数传递一个值。最简单的形式如下:

request.end = function(callback) {
  callback(false);
};

How is the signature { [Function] fail: [Function] } get created?

console.log 如何表示函数尚未标准化。此输出只是告诉您该值是一个具有自定义属性 fail 的函数对象,它也是一个函数。

如果你想自己创造这样的值(value),可以这样做:

function done() {}
done.fail = function() {};

console.log(done) 是否为您提供所看到的输出取决于浏览器和可能的其他实现特定启发法。

关于javascript - JavaScript 如何知道执行失败函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40935543/

相关文章:

javascript - 在表单 id html-editor-form 中显示 HTML 值

php - 数以百万计的列表映射,100GB 数据顺利显示,建议

node.js - package.json 中的 npm 语义版本控制 - 它仅适用于 1.0.0 及更高版本吗?

javascript - 不能在 Electron 中要求 http 协议(protocol)中的渲染器

javascript - 使用 Jasmine 测试 Angular Promise

javascript - Dojo:在事件监听器回调中维护对小部件的引用

javascript - Backbone.js - 集合请求事件未触发

node.js - 从作为子进程运行的脚本将 Node 作为后台进程运行

javascript - 使用 Jasmine 进行 http post 方法的单元测试用例

jquery - 如何使用 Jasmine 对保存时的主干成功和错误响应进行单元测试