如何在 Nodeunit 中添加自定义断言 [英] How to add custom assertions in Nodeunit

查看:41
本文介绍了如何在 Nodeunit 中添加自定义断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法向传递给每个测试的 NodeUnit test 对象添加自定义断言?

Is there a way of adding custom assertions to the NodeUnit test object that gets passed to each test?

我想做类似的事情:

var Test = require('nodeunit').Test;

Test.prototype.customAssertion = function(obj) {
  test.same(obj.foo, 'bar');
  test.same(obj.bar, 'baz');
}

exports.test = function(test) {
  test.customAssertion(obj);

  test.done();
}

推荐答案

var assert = require('nodeunit').assert;
var testCase = require('nodeunit').testCase;

assert.isVowel = function(letter, message) {
    var vowels = [ 'a', 'e', 'i', 'o', 'u' ];

    if (vowels.indexOf(letter) == -1) {
        assert.fail(letter, vowels.toString(), message, 'is not in');
    }
};

exports["Vowel"] = testCase({
    "e should be a vowel": function(test) {
        test.isVowel("e", 'It should be a vowel.');
        test.done();
    }
});

这篇关于如何在 Nodeunit 中添加自定义断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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