【问题标题】:Grunt Plugin - Chai Test AssertionGrunt 插件 - Chai 测试断言
【发布时间】:2014-07-24 21:34:14
【问题描述】:

我有一个 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

【问题讨论】:

    标签: gruntjs npm chai


    【解决方案1】:

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

    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()

    【讨论】:

    • 但我确实想要一个错误。我想要一个非法的文件名来生成错误。
    • 在断言之前console.log(arguments)会看到什么?
    • { '0': [Error: No handler for filetype: test/input/illegal.file.name](我缩短了错误信息)}
    • 像你一样添加或删除空间不应该对 err 是否定义有影响,所以其他的事情从这里并不明显。如果您遇到其他可能有帮助的事情,请告诉我。对不起!
    • 是的,整个测试似乎搞砸了,因为后续运行会产生不同的结果。无论如何,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 2021-12-14
    • 2018-06-26
    • 2017-02-14
    • 2012-07-16
    相关资源
    最近更新 更多