Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有没有办法添加自定义方法来断言chai的接口?_Javascript_Unit Testing_Mocha.js_Chai - Fatal编程技术网

Javascript 有没有办法添加自定义方法来断言chai的接口?

Javascript 有没有办法添加自定义方法来断言chai的接口?,javascript,unit-testing,mocha.js,chai,Javascript,Unit Testing,Mocha.js,Chai,有没有办法添加自定义方法来断言chai的接口 我试过: // test-helper.js export const testPlugin = function(chai) { chai.Assertion.addMethod('customMethod', function() { //Something }) } // abc.test.js import {assert, use} from 'chai' import {testPlugin} from

有没有办法添加自定义方法来断言chai的接口

我试过:

// test-helper.js

export const testPlugin = function(chai) {
    chai.Assertion.addMethod('customMethod', function() {
      //Something
    })
 }

// abc.test.js

import {assert, use} from 'chai'
import {testPlugin} from 'test-helper.js'

use(testPlugin)
但我认为这只适用于chai的expect接口。 我想将此自定义方法用作assert.customMethod(实际、预期)

如果我遗漏了什么,请告诉我

 assert.customMethod = function(actual, expected) {
   //...
};

这就是所谓的猴子修补。

扩展另一个答案。。。请查看和其他本机断言以供参考。您的自定义断言可能如下所示:

const chai = require("chai");
chai.assert.assertSpecial = function (actual) {
    // see https://github.com/chaijs/chai/blob/master/lib/chai/assertion.js
    // for Assertion's argument definitions
    const test = new chai.Assertion(null, null, chai.assert, true);
    test.assert(
        actual === "special",
        `expected ${actual} to be "special"`,
        `expected ${actual} to not be "special"`,
        "special",
        actual,
        true);
};