Grunt Plugin - Chai Test Assertion

时间:2014-07-23 19:24:50

标签: gruntjs npm chai

我有一个Grunt插件,我试图编写测试。我试图测试当我尝试对无效的文件类型执行操作时,我的回调中是否收到错误。错误'变量返回的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"

当我把空间放回到" No"之间时和"处理程序",我得到一个非常奇怪的错误:

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

1 个答案:

答案 0 :(得分:0)

具有相同参数的两个对象不相等。尝试这样的事情:

should.equal(err.message, newError.message)

或者只是跳过创建新错误并执行:

should.equal(err.message, "No handler for filetype. Unsure what to do with this file: test/input/illegal.file.name")

此外,您正在运行异步测试,因此请务必在断言后调用done()