I use assert(...)
on methods to validate arguments, e.g.
我在方法上使用assert(…)来验证参数,例如。
var assert = require('assert')
function someFunction(a, b, c) {
assert(a, 'a required')
assert(b, 'b required')
assert(c, 'c required')
}
I am using tap and would like to write a test to validate that an assert exception was thrown. I cannot just test that another error is thrown, because the assert is essentially an early-guard. The bad input will eventually throw an error.
我正在使用tap并希望编写一个测试来验证抛出了断言异常。我不能只测试另一个错误,因为断言本质上是一个早期的守卫。错误的输入最终会抛出一个错误。
1 个解决方案
#1
3
You compare against an assert.AssertionError
in your t.throws()
e.g.
与断言进行比较。你的t.throw()中的AssertionError ()
var test = require('tap').test
test('calling someFunction without arguments', function(t){
t.throws(function(){
someFunction()
}, new assert.AssertionError({
message: 'a required'
}), 'throws assert error')
t.end()
})
The AssertionError
takes an object in it's constructor whose .message
property will be compared against the thrown exception.
AssertionError在其构造函数中获取一个对象,该对象的.message属性将与抛出的异常进行比较。
#1
3
You compare against an assert.AssertionError
in your t.throws()
e.g.
与断言进行比较。你的t.throw()中的AssertionError ()
var test = require('tap').test
test('calling someFunction without arguments', function(t){
t.throws(function(){
someFunction()
}, new assert.AssertionError({
message: 'a required'
}), 'throws assert error')
t.end()
})
The AssertionError
takes an object in it's constructor whose .message
property will be compared against the thrown exception.
AssertionError在其构造函数中获取一个对象,该对象的.message属性将与抛出的异常进行比较。