Nock无法使用相同的方法处理多个测试用例

问题描述

如果我评论一个测试用例,则第二个测试可以正常工作。如果我运行此测试文件。它给出了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实例。

摩卡文档:https://mochajs.org/#hooks

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...