javascript 为什么我的 mocha/chai Error 抛出测试失败?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26072381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is my mocha/chai Error throwing test failing?
提问by Khrob
I have a simple javascript package I'm trying to test. I want to check for an Error being thrown, but when my test is run, and the error is thrown, the test is marked as failing.
我有一个简单的 javascript 包,我正在尝试测试。我想检查抛出的错误,但是当我的测试运行时,并抛出错误,测试被标记为失败。
Here's the code:
这是代码:
var should = require('chai').should(),
expect = require('chai').expect();
describe('#myTestSuite', function () {
it ('should check for TypeErrors', function () {
// Pulled straight from the 'throw' section of
// http://chaijs.com/api/bdd/
var err = new ReferenceError('This is a bad function.');
var fn = function () { throw err; }
expect(fn).to.throw(ReferenceError);
})
})
Which, when run gives me the following output:
其中,运行时给我以下输出:
kh:testthing khrob$ npm test
> [email protected] test /Users/khrob/testthing
> mocha
#myTestSuite
1) should check for TypeErrors
0 passing (5ms) 1 failing
1) #myTestSuite should check for TypeErrors:
TypeError: object is not a function
at Context.<anonymous> (/Users/khrob/testthing/test/index.js:10:3)
at callFn (/Users/khrob/testthing/node_modules/mocha/lib/runnable.js:249:21)
at Test.Runnable.run (/Users/khrob/testthing/node_modules/mocha/lib/runnable.js:242:7)
at Runner.runTest (/Users/khrob/testthing/node_modules/mocha/lib/runner.js:373:10)
at /Users/khrob/testthing/node_modules/mocha/lib/runner.js:451:12
at next (/Users/khrob/testthing/node_modules/mocha/lib/runner.js:298:14)
at /Users/khrob/testthing/node_modules/mocha/lib/runner.js:308:7
at next (/Users/khrob/testthing/node_modules/mocha/lib/runner.js:246:23)
at Object._onImmediate (/Users/khrob/testthing/node_modules/mocha/lib/runner.js:275:5)
at processImmediate [as _immediateCallback] (timers.js:336:15)
npm ERR! Test failed. See above for more details.
npm ERR! not ok code 0
I know there's dozens of answers on here about what you pass to expect() being a function not the result of a function, and I've tried every permutation of anonymous functionizing I can think of, but I always get the failed test result.
我知道这里有很多关于你传递给 expect() 的答案是一个函数而不是函数的结果,我已经尝试了我能想到的匿名函数化的每一种排列,但我总是得到失败的测试结果。
I'm thinking it must be something to do with my config, given that I'm basically just running the example from the documentation, or my expectation for what is a pass or fail on the test is not calibrated properly.
我认为这一定与我的配置有关,因为我基本上只是在运行文档中的示例,或者我对测试通过或失败的期望没有正确校准。
Any clues?
有什么线索吗?
回答by Steven Vachon
This should fix your problem:
这应该可以解决您的问题:
var expect = require('chai').expect;
Notice that the expect
function is not being invoked.
请注意,该expect
函数没有被调用。
回答by Khrob
Tracked it down!
追踪了它!
Up the top
登顶
expect = require('chai').expect(),
wasn't giving me anything useful. Changing it to:
没有给我任何有用的东西。将其更改为:
chai = require('chai'),
then calling the test as
然后将测试称为
chai.expect(fn).to.throw(ReferenceError);
does exactly what I expected.
完全符合我的预期。
Thanks for the help.
谢谢您的帮助。