小编ple*_*xer的帖子

使用Mocha/Chai和async/await验证是否抛出异常

我正在努力找出在使用async/await时在Mocha测试中验证承诺被拒绝的最佳方法.

这是一个有效的例子,但我不喜欢should.be.rejectedWith返回一个需要从测试函数返回的promise才能正确评估.使用async/await删除了测试值的这个要求(正如我对wins()下面的结果所做的那样),我觉得很可能在某些时候会忘记return语句,在这种情况下测试将始终通过.

// Always succeeds
function wins() {
  return new Promise(function(resolve, reject) {
    resolve('Winner');
  });
}

// Always fails with an error
function fails() {
  return new Promise(function(resolve, reject) {
    reject('Contrived Error');
  });
}

it('throws an error', async () => {
  let r = await wins();
  r.should.equal('Winner');

  return fails().should.be.rejectedWith('Contrived Error');
});
Run Code Online (Sandbox Code Playgroud)

感觉应该可以使用async/await将拒绝转换为异常的事实并将其与Chai的should.throw相结合,但我无法确定正确的语法.

理想情况下这可行,但似乎不是:

it('throws an error', async () => {
  let r = await wins();
  r.should.equal('Winner');

  (await fails()).should.throw(Error);
});
Run Code Online (Sandbox Code Playgroud)

mocha.js node.js async-await chai chai-as-promised

21
推荐指数
5
解决办法
1万
查看次数

"致命错误:无法找到当地的咕噜声." 在Windows 7上

我无法在Windows 7上轻松工作.按照Grunt网站上的说明(http://gruntjs.com/getting-started),我运行:

npm uninstall -g grunt-cli
npm uninstall grunt
npm uninstall -g grunt-init

git clone git@github.com:gruntjs/grunt-init-jquery.git c:/Users/me/.grunt-init/jquery

npm install -g grunt-cli
grunt-init jquery
npm install .
Run Code Online (Sandbox Code Playgroud)

之后,运行"grunt"会产生以下输出:

grunt-cli: The grunt command line interface. (v0.1.9)

Fatal error: Unable to find local grunt.

If you're seeing this message, either a Gruntfile wasn't found or grunt
hasn't been installed locally to your project. For more information about
installing and configuring grunt, please see the Getting Started guide:

http://gruntjs.com/getting-started
Run Code Online (Sandbox Code Playgroud)

npm list grunt的输出是: …

gruntjs

5
推荐指数
1
解决办法
7401
查看次数