我有一个 Grunt 插件,我正在尝试为其编写测试。我正在尝试测试当我尝试对无效文件类型执行操作时,我的回调中是否出现错误。'err' 变量返回的 toString() 值为:
[Error: No handler for filetype. Unsure what to do with this file: test/input/illegal.file.name
但是当我尝试将此作为字符串进行测试时,我得到了我怀疑的类型错误。
it('should return an error for illegal file names', function(done) {
grunt.task([
{
src : ['test/input/illegal.file.name'],
dest : '.tmp/ignored.out'
},
], function(err) {
should.equal(err, "Error: No handler for filetype. Unsure what to do with this file: test/input/illegal.file.name");
});
});
这将返回:
AssertionError: expected [Error: No handler for filetype. Unsure what to do with this file: test/input/illegal.file.name] to equal 'Error: No handler for filetype. Unsure what to do with this file: test/input/illegal.file.name'
因此,我尝试创建一个新错误并与之匹配,但无济于事:
it('should return an error for illegal file names', function(done) {
grunt.task([
{
src : ['test/input/illegal.file.name'],
dest : '.tmp/ignored.out'
},
], function(err) {
var newError = new Error("No handler for filetype. Unsure what to do with this file: test/input/illegal.file.name");
should.equal(err, newError);
});
});
但这会返回:
AssertionError: expected
[Error: No handler for filetype. Unsure what to do with this file: test/input/illegal.file.name] to equal
[Error: No handler for filetype. Unsure what to do with this file: test/input/illegal.file.name]
+ expected - actual
这似乎是平等的,但我显然错过了一些非常基本的东西......
任何帮助将不胜感激。
更新
发生了一些非常奇怪的事情,因为如果我从测试字符串中取出一个空格,我会收到一条非常有效的消息:
it('should return an error for illegal file names', function(done) {
grunt.task([
{
src : ['test/input/illegal.file.name'],
dest : '.tmp/ignored.out'
},
], function(err) {
// v-- no space
should.equal(err.message, "Nohandler for filetype: test/input/illegal.file.name");
});
});
回报:
AssertionError: expected 'No handler for filetype: test/input/illegal.file.name' to equal 'Nohandler for filetype: test/input/illegal.file.name'
+ expected - actual
+"Nohandler for filetype: test/input/illegal.file.name"
-"No handler for filetype: test/input/illegal.file.name"
当我把空格放回“否”和“处理程序”之间时,我得到一个非常奇怪的错误:
it('should return an error for illegal file names', function(done) {
grunt.task([
{
src : ['test/input/illegal.file.name'],
dest : '.tmp/ignored.out'
},
], function(err) {
should.equal(err.message, "No handler for filetype: test/input/illegal.file.name");
});
});
这将返回:
1) should return an error for illegal file names:
TypeError: Cannot read property 'message' of undefined