简体   繁体   English

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

[英]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. 测试用例用javascript编写。 The idea would be to run the code inside the AfterEach structure. 我们的想法是在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
    });
  });
});

Inside beforeEach() 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. 这样,您将有合同的每一个的新实例it的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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