如何使用新的断言函数扩展QUnit? [英] How do I extend QUnit with new assertion functions?

查看:96
本文介绍了如何使用新的断言函数扩展QUnit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向QUnit添加新的断言。
我做了这样的事情:

I'd like to add new assertions to QUnit. I've done something this:

QUnit.extend(QUnit.assert, {
  increases: function(measure, block, message){
    var before = measure();
    block();
    var after = measure();
    var passes = before < after;
    QUnit.push(passes, after, "< " + before, message);
  }
});

当我使用增加时(foo,bar,baz)在我的测试中,我得到了

When I use increases(foo,bar,baz) in my test, I get


ReferenceError:未定义增量

ReferenceError: increases is not defined

从浏览器控制台我可以看到增加中找到QUnit.assert 以及所有其他标准功能: ok 等于 deepEqual 等。

From the browser console I can see increases is found in QUnit.assert along with all the other standard functions: ok, equal, deepEqual, etc.

从控制台运行:

test(foo,function(){console .log(ok)});

我看到 ok的来源

正在运行:

test(foo,function(){console.log(increase)});

我被告知增加没有定义。

Running:
test("foo", function(){console.log(increases) });
I am told increases is not defined.

在测试中使用增加所需的魔力是什么?此外,哪里(如果有的话)是文件?

What is the magic required to use my increases in a test? Also, where (if anywhere) is the documentation for that?

谢谢

推荐答案

我发现解决方案是接受一个测试回调函数中的参数。该参数将具有额外的断言类型。所以我们可以像这样调用它:

I have found the solution is to accept a parameter in the test callback function. That parameter will have the extra assertion type. So we can call it like so:

//the assert parameter accepted by the callback will contain the 'increases' assertion
test("adding 1 increases a number", function(assert){
    var number = 42;
    function measure(){return number;}
    function block(){number += 1;}
    assert.increases(measure, block);
});

这篇关于如何使用新的断言函数扩展QUnit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆