问题描述
如果我评论第一个测试用例,则第二个测试可以正常工作。如果我运行此测试文件。它给出了400状态,这是第一个测试用例的状态。如何运行两个测试用例。
after(function () {
nock.cleanAll()
})
account.js
describe("Account",() => {
describe("/PATCH account/changePassword",() => {
before(function(done){
nock(baseUrl)
.persist()
.patch('/account/changePassword')
.reply(400)
done()
// console.log(nock.activeMocks())
})
it("it should give validation error",(done) => {
chai.request(baseUrl)
.patch('/account/changePassword')
.end((err,res) => {
res.should.have.status(400);
done();
});
})
})
//===================== 2nd test case with same method================
describe("/PATCH account/changePassword",() => {
before(function(done){
nock(baseUrl)
.intercept('/account/changePassword','PATCH')
.reply(200,{ data: {"password": "123456"} })
done()
// console.log(nock.activeMocks())
})
it("it should update the password",(done) => {
chai.request(baseUrl)
.patch('/account/changePassword')
.send({"password": "123456"})
.end((err,res) => {
console.log(res);
res.should.have.status(200);
done();
});
})
})
});
解决方法
您应该使用Mocha的afterEach
钩子。 after
仅在所有测试运行之后才运行,因此,当第二个测试运行时,您当前拥有一个持久的Nock实例。