简体   繁体   中英

Cleaning smart contract storage after running a test case

When running test cases for a Smart Contract I would like to either destroy the smart contract and redeploy it or reset its state after each test case is run. The test cases are written in javascript. The idea would be to run the code inside the AfterEach structure.


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
    });
  });
});

Inside beforeEach()

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

This way you will have a new instance of Contract for each it case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM