在 运行 个测试用例后清理智能合约存储

Cleaning smart contract storage after running a test case

当 运行 为智能合约设置测试用例时,我想销毁智能合约并重新部署它或在每个测试用例 运行 后重置其状态。测试用例写在javascript。这个想法是 运行 AfterEach 结构中的代码。


contract("Contract", accounts => {
  let contract;
  let owner = accounts[0];
  let admin = accounts[1];
  let user = accounts[2];

  describe("function1 tests", () => {
    before("Setup contract for each test", async () => {
      contract = await Contract.deployed();
    });

    afterEach("", async () => {
     //code to selfdestruct or reset the state of the contract after 
     //each test
    });

    it("test1", () => {
      //test1 code
    });

    it("test2", () => {
      //test2 code
    });
  });
});

里面beforeEach()

beforeEach('initialize the contract', async function() {
    contract = await Contract.new()
})

这样,您将为每个 it 个案例创建一个新的 Contract 实例。