首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用chai-chai和sinon测试浮点逻辑

使用chai-chai和sinon测试浮点逻辑
EN

Stack Overflow用户
提问于 2019-05-21 05:37:27
回答 1查看 367关注 0票数 0

我有一个测试用例失败了,因为被测试的值被Number.EPSILON关闭了。我理解为什么会发生这种情况,并相信我需要修改我的测试用例,以便它能够容忍这种差异。我相信使用chai-almost来帮助实现这一点是有意义的,但我正在努力弄清楚如何将chai-almostsinon-chai集成在一起,并且正在寻找想法。

具体来说,我使用的是sinon-chai提供的calledWithMatch方法。calledWithMatch方法在两个对象之间执行深度相等检查,并且不考虑引用相等性。我想放宽这个方法来容忍Number.EPSILON的差异。

下面的代码片段突出显示了一个失败的测试用例的问题。测试用例失败是因为persist调用了一个边界框,由于topNumber.EPSILON关闭,这个边界框没有达到我们的预期。在这种情况下,测试用例应该通过,因为数据没有任何错误。

代码语言:javascript
复制
mocha.setup('bdd');

const updater = {
  updateBoundingBox(boundingBox) {
    const newBoundingBox = { ...boundingBox };
    newBoundingBox.top -= .2;
    newBoundingBox.top += .2;  
    this.persist(newBoundingBox);
  },
  
  persist(boundingBox) {
    console.log('persisting bounding box', boundingBox);
  }
};

describe('example', () => {
  it('should pass', () => {
    const persistSpy = sinon.spy(updater, 'persist');

    const originalBoundingBox = {
      top: 0.01,
      left: 0.01,
      bottom: 0.01,
      right: 0.01,
    };
    updater.updateBoundingBox(originalBoundingBox);
    chai.expect(persistSpy).calledWithMatch(originalBoundingBox);
  });
});

mocha.run();
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/6.1.4/mocha.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.2.0/chai.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sinon.js/7.3.2/sinon.min.js"></script>
<script>
"use strict";
/* eslint-disable no-invalid-this */

(function (sinonChai) {
    // Module systems magic dance.

    /* istanbul ignore else */
    if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
        // NodeJS
        module.exports = sinonChai;
    } else if (typeof define === "function" && define.amd) {
        // AMD
        define(function () {
            return sinonChai;
        });
    } else {
        // Other environment (usually <script> tag): plug in to global chai instance directly.
        /* global chai: false */
        chai.use(sinonChai);
    }
}(function (chai, utils) {
    var slice = Array.prototype.slice;

    function isSpy(putativeSpy) {
        return typeof putativeSpy === "function" &&
               typeof putativeSpy.getCall === "function" &&
               typeof putativeSpy.calledWithExactly === "function";
    }

    function timesInWords(count) {
        switch (count) {
            case 1: {
                return "once";
            }
            case 2: {
                return "twice";
            }
            case 3: {
                return "thrice";
            }
            default: {
                return (count || 0) + " times";
            }
        }
    }

    function isCall(putativeCall) {
        return putativeCall && isSpy(putativeCall.proxy);
    }

    function assertCanWorkWith(assertion) {
        if (!isSpy(assertion._obj) && !isCall(assertion._obj)) {
            throw new TypeError(utils.inspect(assertion._obj) + " is not a spy or a call to a spy!");
        }
    }

    function getMessages(spy, action, nonNegatedSuffix, always, args) {
        var verbPhrase = always ? "always have " : "have ";
        nonNegatedSuffix = nonNegatedSuffix || "";
        if (isSpy(spy.proxy)) {
            spy = spy.proxy;
        }

        function printfArray(array) {
            return spy.printf.apply(spy, array);
        }

        return {
            affirmative: function () {
                return printfArray(["expected %n to " + verbPhrase + action + nonNegatedSuffix].concat(args));
            },
            negative: function () {
                return printfArray(["expected %n to not " + verbPhrase + action].concat(args));
            }
        };
    }

    function sinonProperty(name, action, nonNegatedSuffix) {
        utils.addProperty(chai.Assertion.prototype, name, function () {
            assertCanWorkWith(this);

            var messages = getMessages(this._obj, action, nonNegatedSuffix, false);
            this.assert(this._obj[name], messages.affirmative, messages.negative);
        });
    }

    function sinonPropertyAsBooleanMethod(name, action, nonNegatedSuffix) {
        utils.addMethod(chai.Assertion.prototype, name, function (arg) {
            assertCanWorkWith(this);

            var messages = getMessages(this._obj, action, nonNegatedSuffix, false, [timesInWords(arg)]);
            this.assert(this._obj[name] === arg, messages.affirmative, messages.negative);
        });
    }

    function createSinonMethodHandler(sinonName, action, nonNegatedSuffix) {
        return function () {
            assertCanWorkWith(this);

            var alwaysSinonMethod = "always" + sinonName[0].toUpperCase() + sinonName.substring(1);
            var shouldBeAlways = utils.flag(this, "always") && typeof this._obj[alwaysSinonMethod] === "function";
            var sinonMethodName = shouldBeAlways ? alwaysSinonMethod : sinonName;

            var messages = getMessages(this._obj, action, nonNegatedSuffix, shouldBeAlways, slice.call(arguments));
            this.assert(
                this._obj[sinonMethodName].apply(this._obj, arguments),
                messages.affirmative,
                messages.negative
            );
        };
    }

    function sinonMethodAsProperty(name, action, nonNegatedSuffix) {
        var handler = createSinonMethodHandler(name, action, nonNegatedSuffix);
        utils.addProperty(chai.Assertion.prototype, name, handler);
    }

    function exceptionalSinonMethod(chaiName, sinonName, action, nonNegatedSuffix) {
        var handler = createSinonMethodHandler(sinonName, action, nonNegatedSuffix);
        utils.addMethod(chai.Assertion.prototype, chaiName, handler);
    }

    function sinonMethod(name, action, nonNegatedSuffix) {
        exceptionalSinonMethod(name, name, action, nonNegatedSuffix);
    }

    utils.addProperty(chai.Assertion.prototype, "always", function () {
        utils.flag(this, "always", true);
    });

    sinonProperty("called", "been called", " at least once, but it was never called");
    sinonPropertyAsBooleanMethod("callCount", "been called exactly %1", ", but it was called %c%C");
    sinonProperty("calledOnce", "been called exactly once", ", but it was called %c%C");
    sinonProperty("calledTwice", "been called exactly twice", ", but it was called %c%C");
    sinonProperty("calledThrice", "been called exactly thrice", ", but it was called %c%C");
    sinonMethodAsProperty("calledWithNew", "been called with new");
    sinonMethod("calledBefore", "been called before %1");
    sinonMethod("calledAfter", "been called after %1");
    sinonMethod("calledImmediatelyBefore", "been called immediately before %1");
    sinonMethod("calledImmediatelyAfter", "been called immediately after %1");
    sinonMethod("calledOn", "been called with %1 as this", ", but it was called with %t instead");
    sinonMethod("calledWith", "been called with arguments %*", "%D");
    sinonMethod("calledOnceWith", "been called exactly once with arguments %*", "%D");
    sinonMethod("calledWithExactly", "been called with exact arguments %*", "%D");
    sinonMethod("calledOnceWithExactly", "been called exactly once with exact arguments %*", "%D");
    sinonMethod("calledWithMatch", "been called with arguments matching %*", "%D");
    sinonMethod("returned", "returned %1");
    exceptionalSinonMethod("thrown", "threw", "thrown %1");
}));
</script>

<div id="mocha"></div>

我真的不确定下一步该怎么做。如果我直接使用这两个实体,而不是使用calledWithMatch,我将使用chai-almost显式地检查topbottomleftright的值。类似于:

代码语言:javascript
复制
expect(newBoundingBox.top).to.almost.equal(boundingBox.top)
expect(newBoundingBox.bottom).to.almost.equal(boundingBox.bottom)
expect(newBoundingBox.left).to.almost.equal(boundingBox.left)
expect(newBoundingBox.right).to.almost.equal(boundingBox.right)

但在使用calledWithMatch时,我看不到一种方法来实现这一点。

我是不是遗漏了什么?有没有简单的方法来解决这个问题呢?

编辑:在我修补的时候更新这个。

我认为正确的方法可能是使用自定义匹配器,但我还没有可用的代码:https://sinonjs.org/releases/latest/matchers/#custom-matchers

看起来calledWithMatch(foo)的功能等价物是calledWith(sinon.match(foo)),这使得如何引入自定义匹配器的用法变得更加清晰。

EN

回答 1

Stack Overflow用户

发布于 2019-05-21 06:15:16

好吧,我想通了。

诀窍是将sinon-chai方法calledWithMatch替换为其较低级别的实现calledWith(sinon.match。这允许用户定义一个自定义的匹配器。我展示了我在下面的例子中使用的那个。

代码语言:javascript
复制
mocha.setup('bdd');

const updater = {
  updateBoundingBox(boundingBox) {
    const newBoundingBox = { ...boundingBox };
    newBoundingBox.top -= .2;
    newBoundingBox.top += .2;  
    this.persist(newBoundingBox);
  },
  
  persist(boundingBox) {
    console.log('persisting bounding box', boundingBox);
  }
};

describe('example', () => {
  it('should pass', () => {
    const persistSpy = sinon.spy(updater, 'persist');

    const originalBoundingBox = {
      top: 0.01,
      left: 0.01,
      bottom: 0.01,
      right: 0.01,
    };
    updater.updateBoundingBox(originalBoundingBox);
    chai.expect(persistSpy).calledWith(sinon.match((boundingBox) => {
        if (!boundingBox) return false;
        
        const isLeftEqual = Math.abs(originalBoundingBox.left - boundingBox.left) < Number.EPSILON;
        const isRightEqual = Math.abs(originalBoundingBox.right - boundingBox.right) < Number.EPSILON;
        const isTopEqual = Math.abs(originalBoundingBox.top - boundingBox.top) < Number.EPSILON;
        const isBottomEqual = Math.abs(originalBoundingBox.bottom - boundingBox.bottom) < Number.EPSILON;
        
        return isLeftEqual && isRightEqual && isTopEqual && isBottomEqual; 
    }));
  });
});

mocha.run();
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/6.1.4/mocha.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.2.0/chai.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sinon.js/7.3.2/sinon.min.js"></script>
<script>
"use strict";
/* eslint-disable no-invalid-this */

(function (sinonChai) {
    // Module systems magic dance.

    /* istanbul ignore else */
    if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
        // NodeJS
        module.exports = sinonChai;
    } else if (typeof define === "function" && define.amd) {
        // AMD
        define(function () {
            return sinonChai;
        });
    } else {
        // Other environment (usually <script> tag): plug in to global chai instance directly.
        /* global chai: false */
        chai.use(sinonChai);
    }
}(function (chai, utils) {
    var slice = Array.prototype.slice;

    function isSpy(putativeSpy) {
        return typeof putativeSpy === "function" &&
               typeof putativeSpy.getCall === "function" &&
               typeof putativeSpy.calledWithExactly === "function";
    }

    function timesInWords(count) {
        switch (count) {
            case 1: {
                return "once";
            }
            case 2: {
                return "twice";
            }
            case 3: {
                return "thrice";
            }
            default: {
                return (count || 0) + " times";
            }
        }
    }

    function isCall(putativeCall) {
        return putativeCall && isSpy(putativeCall.proxy);
    }

    function assertCanWorkWith(assertion) {
        if (!isSpy(assertion._obj) && !isCall(assertion._obj)) {
            throw new TypeError(utils.inspect(assertion._obj) + " is not a spy or a call to a spy!");
        }
    }

    function getMessages(spy, action, nonNegatedSuffix, always, args) {
        var verbPhrase = always ? "always have " : "have ";
        nonNegatedSuffix = nonNegatedSuffix || "";
        if (isSpy(spy.proxy)) {
            spy = spy.proxy;
        }

        function printfArray(array) {
            return spy.printf.apply(spy, array);
        }

        return {
            affirmative: function () {
                return printfArray(["expected %n to " + verbPhrase + action + nonNegatedSuffix].concat(args));
            },
            negative: function () {
                return printfArray(["expected %n to not " + verbPhrase + action].concat(args));
            }
        };
    }

    function sinonProperty(name, action, nonNegatedSuffix) {
        utils.addProperty(chai.Assertion.prototype, name, function () {
            assertCanWorkWith(this);

            var messages = getMessages(this._obj, action, nonNegatedSuffix, false);
            this.assert(this._obj[name], messages.affirmative, messages.negative);
        });
    }

    function sinonPropertyAsBooleanMethod(name, action, nonNegatedSuffix) {
        utils.addMethod(chai.Assertion.prototype, name, function (arg) {
            assertCanWorkWith(this);

            var messages = getMessages(this._obj, action, nonNegatedSuffix, false, [timesInWords(arg)]);
            this.assert(this._obj[name] === arg, messages.affirmative, messages.negative);
        });
    }

    function createSinonMethodHandler(sinonName, action, nonNegatedSuffix) {
        return function () {
            assertCanWorkWith(this);

            var alwaysSinonMethod = "always" + sinonName[0].toUpperCase() + sinonName.substring(1);
            var shouldBeAlways = utils.flag(this, "always") && typeof this._obj[alwaysSinonMethod] === "function";
            var sinonMethodName = shouldBeAlways ? alwaysSinonMethod : sinonName;

            var messages = getMessages(this._obj, action, nonNegatedSuffix, shouldBeAlways, slice.call(arguments));
            this.assert(
                this._obj[sinonMethodName].apply(this._obj, arguments),
                messages.affirmative,
                messages.negative
            );
        };
    }

    function sinonMethodAsProperty(name, action, nonNegatedSuffix) {
        var handler = createSinonMethodHandler(name, action, nonNegatedSuffix);
        utils.addProperty(chai.Assertion.prototype, name, handler);
    }

    function exceptionalSinonMethod(chaiName, sinonName, action, nonNegatedSuffix) {
        var handler = createSinonMethodHandler(sinonName, action, nonNegatedSuffix);
        utils.addMethod(chai.Assertion.prototype, chaiName, handler);
    }

    function sinonMethod(name, action, nonNegatedSuffix) {
        exceptionalSinonMethod(name, name, action, nonNegatedSuffix);
    }

    utils.addProperty(chai.Assertion.prototype, "always", function () {
        utils.flag(this, "always", true);
    });

    sinonProperty("called", "been called", " at least once, but it was never called");
    sinonPropertyAsBooleanMethod("callCount", "been called exactly %1", ", but it was called %c%C");
    sinonProperty("calledOnce", "been called exactly once", ", but it was called %c%C");
    sinonProperty("calledTwice", "been called exactly twice", ", but it was called %c%C");
    sinonProperty("calledThrice", "been called exactly thrice", ", but it was called %c%C");
    sinonMethodAsProperty("calledWithNew", "been called with new");
    sinonMethod("calledBefore", "been called before %1");
    sinonMethod("calledAfter", "been called after %1");
    sinonMethod("calledImmediatelyBefore", "been called immediately before %1");
    sinonMethod("calledImmediatelyAfter", "been called immediately after %1");
    sinonMethod("calledOn", "been called with %1 as this", ", but it was called with %t instead");
    sinonMethod("calledWith", "been called with arguments %*", "%D");
    sinonMethod("calledOnceWith", "been called exactly once with arguments %*", "%D");
    sinonMethod("calledWithExactly", "been called with exact arguments %*", "%D");
    sinonMethod("calledOnceWithExactly", "been called exactly once with exact arguments %*", "%D");
    sinonMethod("calledWithMatch", "been called with arguments matching %*", "%D");
    sinonMethod("returned", "returned %1");
    exceptionalSinonMethod("thrown", "threw", "thrown %1");
}));
</script>

<div id="mocha"></div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56228486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档