摩卡,nodejs承诺testing无法完成,因为缺乏完成
我试图运行一个承诺的testing,但testing失败,认为它超过超时限制,并build议确保我已经完成的条款。
这是我的testing代码的一部分:
$configurations .updateConfiguration(configurations_driver.NOT_VALID_MODEL) //invalid model .then(function () { done(new Error("Expected INVALID_MODEL error but got OK")); }, function (error) { chai.assert.isNotNull(error); chai.expect(error.message).to.be.eq("INVALID_MODEL_ERROR"); chai.expect(error.kind).to.be.eq("ERROR_KIND"); chai.expect(error.path).to.be.eq("ERROR_PATH"); done(); }) .catch(done); });
我已经完成了所有的子句,所以我不知道在testing中是否遗漏了某些东西,或者结构是错误的。
摩卡支持testing承诺,只要你答复,就不用done
。
const expect = chai.expect it('should error', function(){ return $configurations .updateConfiguration(configurations_driver.NOT_VALID_MODEL) //invalid model .then(()=> { throw new Error("Expected INVALID_MODEL error but got OK")}) .catch(error => { expect(error).to.not.be.null; expect(error.message).to.equal("INVALID_MODEL_ERROR"); expect(error.kind).to.equal("ERROR_KIND"); expect(error.path).to.equal("ERROR_PATH"); }) })
还要看看chai-as-promised
promise承诺testing更像标准的chai断言/期望。
chai.should() chai.use(require('chai-as-promised')) it('should error', function(){ return $configurations .updateConfiguration(configurations_driver.NOT_VALID_MODEL) .should.be.rejectedWith(/INVALID_MODEL_ERROR/) })
在节点7.6+环境或者你有babel / babel-register的地方,你也可以使用async
/ await
promise处理程序
it('should error', async function(){ try { await $configurations.updateConfiguration(configurations_driver.NOT_VALID_MODEL) throw new Error("Expected INVALID_MODEL error but got OK")}) } catch (error) { expect(error).to.not.be.null; expect(error.message).to.equal("INVALID_MODEL_ERROR"); expect(error.kind).to.equal("ERROR_KIND"); expect(error.path).to.equal("ERROR_PATH"); } })
- 用Chai,Mocha,Express,johnny-five和node编写testing时的菜鸟错误
- asynchronous摩卡testing(与Chai断言库)应该失败,但被标记为通过
- 用chai发送一个POST请求发送一个空的正文?
- 如何unit testing连接到mongo的方法,而不实际连接到mongo?
- 使用Mocha和Chai编写testing用例,以便使用Error
- 如何通过摩卡和柴匹配在网页上呈现的特定string
- Chai / Moka – > TypeError:request.get(…).expect不是一个函数
- mongoose模式unit testing失败与前保存挂钩
- 柴期望抛出一个(错误)不通过testing(使用节点)