如何用磁带为测试节点或javascript编写自定义断言,或者检查子字符串而不是t.deepEquals()
How to write a custom assertion for testing node or javascript with tape, or check for substring rather than t.deepEquals()?
使用磁带,我如何编写一个自定义断言方法来代替t.equal()?或者有没有一种测试断言方法可以检查子字符串,这样我测试的整个字符串就不必使用t.deepEqual()进行逐字比较?
var test = require("tape")
test('messages contain key words', function (t) {
// this is what I'm using:
t.equal(MyEncode(Fruit).indexOf('eat more') > -1,true,'should contain "eat more"')
// this is what I want:
t.contains(myEncode(Fruit),'eat more','should contain "eat more"')
t.end()
})
当我测试myEncode时,我可以看到字符串不包含子字符串,但我无法查看实际值,因为它的计算结果仅为false,这没有信息:
not ok 1 should contain "eat more"
---
operator: equal
expected: true
actual: false
at: checkCmd (/test.js:63:11)
...
通过阅读上面的测试输出,我不知道我的测试是不是限制性太强,还是输出实际上不正确。相反,我希望看到myEncode返回的实际值,以加快查找问题的速度:
not ok 2 should contain "eat more"
---
operator: contains
expected: "eat more"
actual: "Apples are allowed to be eaten on weekdays and weekends"
at: checkCmd (/test.js:66:11)
...
多亏了github tape
问题中的@mbstock示例测试方法,我才开始工作。如何定义新的测试方法#154.
要测试的函数,通常在另一个文件(例如appToTest.js)中
function brownFox(argument) {
return "The "+argument+" fox jumped"
}
exports.brownFox = brownFox
测试脚本:
// Test module typically named test.js
var test = require("tape")
var f = require("./appToTest")
// regular built-in test
test('String must match exactly', function (t) { // can be too specific
t.deepEqual(f.brownFox('quick brown'), "The quick brown fox jumped") // should pass
t.deepEqual(f.brownFox('quick black'), "The quick brown fox jumped") // will fail
t.deepEqual(f.brownFox('quick white'), "The quick white fox jumped") // should pass
t.comment('Strings must be fully specified to match word for word')
t.end()
})
// It can be too tedious to maintain the test string to always
// match exactly the string in the code being tested
// I don't care what the fox is as long as it jumped
// Since there is no "contains" test in tape, I hack t.equal()
test('String should contain a fox', function (t) {
// does not show actual value when test fails; shows true or false instead
// "jumped" must be tediously repeated to know what the test is about
t.equal(f.brownFox('quick brown').indexOf("jumped") > -1, true, "contains jumped") // should pass
t.equal(f.brownFox('quick black').indexOf("junped") > -1, true, "contains jumped") // should fail
t.comment('failures are not descriptive')
t.end()
})
// Using equal() can result in more time spent fixing or adjusting the test
// than coding the application
// So define your own tape test method
/////////////// Example of defining a custom tape test method
test.Test.prototype.stringContains = function(actual, contents, message) {
this._assert(actual.indexOf(contents) > -1, {
message: message || 'should contain '+contents,
operator: 'stringContains',
actual: actual,
expected: contents
});
};
/////////////// Example using a custom tape test method
test('String should contain a fox', function (t) {
// shows actual value when test fails
t.stringContains(f.brownFox('quick brown'), "jumped") // should pass
t.stringContains(f.brownFox('quick brown'), "junped") // should fail
// still supports custom message to override default message:
t.stringContains(f.brownFox('quick brown'), "jumped", 'must contain "jumped"') // should pass
t.stringContains(f.brownFox('quik white'), "jumped") // should pass
t.comment('failures are more descriptive')
t.end()
})
请注意,定制的测试输出现在报告"实际"字符串:
TAP version 13
# String must match exactly
ok 1 should be equivalent
not ok 2 should be equivalent
---
operator: deepEqual
expected: 'The quick brown fox jumped'
actual: 'The quick black fox jumped'
at: Test.<anonymous> (./tape-test.js:9:7)
...
ok 3 should be equivalent
# Strings must be fully specified to match word for word
# String should contain a fox
ok 4 contains jumped
not ok 5 contains jumped
---
operator: equal
expected: true
actual: false
at: Test.<anonymous> (./tape-test.js:23:7)
...
# failures are not descriptive
# String should contain a fox
ok 6 should contain jumped
not ok 7 should contain junped
---
operator: stringContains
expected: 'junped'
actual: 'The quick brown fox jumped'
at: Test.test.Test.stringContains (./tape-test.js:33:8)
ok 8 must contain "jumped"
ok 9 should contain jumped
# failures are more descriptive
1..9
# tests 9
# pass 6
# fail 3
我安装了extend-tape
,然后意识到我必须将其与babel
一起使用,因为V8还不支持导入,但我不想添加babel
作为依赖项。
由于上面的代码是有效的,所以我不认为使用extend-tape
有什么意义。
相关文章:
- 如何检查字符串的一部分与数组匹配
- 三元运算符检查多个字符串
- 检查数组中是否存在字符串值,并返回找到的数组值js
- 如何检查一个字符串的所有字符是否都存在于另一个字符串中
- Regex模式,用于检查字符串中每个单词的第一个字母(如果是Javascript中的大写字母)
- 检查变量是否为字符串的简单方法
- 如何检查两个字符串是否具有相同的字符,包括特殊字符
- 将数据作为字符串从C#发送到Javascript-如何检查字符串
- 使用JQuery检查数组的索引中是否存在字符串
- 需要javascript正则表达式来检查以U或C开头、长度为10的字符串
- JavaScript 字符串字母和数字检查
- jQuery检查字符串前面是否有其他字母
- 如何检查数据表是否有任何包含字符串的行并获取它的行索引
- 如何在 AngularJS 中检查字符串是否为空
- 简单的PHP字符串检查不起作用
- 如何根据字符串检查ajax响应
- Javascript 子字符串检查
- 快速字符串检查查找字符串中的重复项
- 简单的JavaScript空字符串检查,(空字符串)是什么意思,为什么失败
- 无法使用字符串检查相等的数字