QUnit 测试不适用于assert.throws()

问题描述 投票:0回答:1

也许我不明白文档,我的测试不起作用。

我将测试函数中的预期错误

// file_to_test.js
...
myFunctionToTest: function(param1, param2 = "AA-BB") {
  const idx1 = param2.indexOf("AA");
  if(idx1 === -1) {
    throw new Error('test message error');
  }
}

来了测试

// test.js
sap.ui.define(["path/to/my/script"], function(myScriptToTest) {
  ...
  QUnit.test('should thrown error', assert => {
    assert.throws(
      function() {
        myScriptToTest.myFunctionToTest('blabla', 'blubblub'); // test is broken at this line
      },
      function(error) {
        return error.toString() === 'test message error';
      },
      'Error thrown'
    }
  })
})

返回(浏览器-)输出:

1. Error thrown
Expected:   function( a ){
  [code]
}
  Result: Error("test message error")
  Diff: function( a ){
          [code]
        }Error("test message error")
Source: ...

如何正确设置测试

sapui5 qunit
1个回答
0
投票

您的 QUnit 代码看起来不错,但您编写 JavaScript 逻辑的方式存在错误。

事实上

error.toString()
不等于
test message error
。您要找的是
error.message
而不是
error.toString()

考虑以下因素:

var myerror = new Error('hello world');
console.log(myerror.toString());
// 'Error: hello world'
console.log(myerror.toString() === 'hello world');
// false

console.log('-------');

console.log(myerror.message);
// 'hello world'
console.log(myerror.message === 'hello world');
// true

另请参阅:

© www.soinside.com 2019 - 2024. All rights reserved.