首页 > 解决方案 > 松露测试:返回错误:处理事务时出现虚拟机异常:恢复

问题描述

我已经多次看到这个错误。网上没有任何东西可以帮助解决它

我连续多次使用相同的函数 startProposalRegistration 来测试有或没有限制的不同场景。

如果帐户不是所有者(onlyOwner),我正在测试我的修改器是否按预期工作。

交易恢复,但我没有得到太多解释为什么交易会恢复,除了它被执行了几次

  context("startProposalRegistration", () => {
    it("should be status == ProposalsRegistrationStarted", async () => {
      await votingInstance.startProposalRegistration({ from: owner })
      const status = await votingInstance.status.call()
      assert.equal(status.toNumber(), 1)
    })
    // it("should not be able to startProposalRegistration as non owner", async () => {
    //   assert.equal(await votingInstance.startProposalRegistration({ from: account2 }), false);

    // })
  })

  context("add/delete proposal", () => {
    it("should be able to add proposal if status == ProposalsRegistrationStarted", async () => {
      await votingInstance.startProposalRegistration({ from: owner })
      assert.ok(await votingInstance.addProposal("Some proposal", { from: whitelisted }))
    })
    // it("should not be able to add proposal if status != ProposalsRegistrationStarted", async () => {
    //   await votingInstance.startProposalRegistration({ from: account2 })

    // })
  })

test/Voting.js(完整源代码)

const { assert } = require("chai");
const should = require("chai").should();
const Voting = artifacts.require("Voting");

contract("Voting", accounts => {
  let [account1, account2] = accounts;
  let [, , address3, address4] = accounts;
  let owner = account1;
  let whitelisted = address4;
  let votingInstance;
  beforeEach(async () => {
    votingInstance = await Voting.deployed();
    await votingInstance.addVoter(whitelisted, { from: owner }); //adding to whitelist
  });

  it("should deploy", async () => {
    should.exist(votingInstance.address)
  });

  it("should be status == RegisteringVoters", async () => {
    const status = await votingInstance.status.call()
    assert.equal(status.toNumber(), 0)
  })

  context("add/delete Voter", () => {
    it("should be able to add voter as a owner", async () => {
      await votingInstance.addVoter(address3, { from: owner })
      const voter = await votingInstance.whiteList.call(address3);
      assert(voter);
    })
    it("should not be able to add voter as a non owner", async () => {
      try {
        await votingInstance.addVoter(address4, { from: account2 })
        assert(false);
      } catch (err) {
        should.exist(err);
      }
    })
    it("should be able to delete voter as a owner", async () => {
      await votingInstance.deleteVoter(address3, { from: owner })
      const voter = await votingInstance.whiteList.call(address3);
      should.not.exist(voter.address);
    })
    it("should not be able to delete voter as non owner", async () => {
      try {
        await votingInstance.deleteVoter(address3, { from: account2 })
        assert(false);
      } catch (err) {
        should.exist(err);
      }
    })
  })

  context("startProposalRegistration", () => {
    it("should be status == ProposalsRegistrationStarted", async () => {
      await votingInstance.startProposalRegistration({ from: owner })
      const status = await votingInstance.status.call()
      assert.equal(status.toNumber(), 1)
    })
    // it("should not be able to startProposalRegistration as non owner", async () => {
    //   assert.equal(await votingInstance.startProposalRegistration({ from: account2 }), false);

    // })
  })

  context("add/delete proposal", () => {
    it("should be able to add proposal if status == ProposalsRegistrationStarted", async () => {
      await votingInstance.startProposalRegistration({ from: owner })
      assert.ok(await votingInstance.addProposal("Some proposal", { from: whitelisted }))
    })
    // it("should not be able to add proposal if status != ProposalsRegistrationStarted", async () => {
    //   await votingInstance.startProposalRegistration({ from: account2 })

    // })
  })

})

和合约 合约/Voting.sol

我遇到问题的功能:

function startProposalRegistration() public onlyOwner   {
        status = WorkflowStatus.ProposalsRegistrationStarted;
        emit ProposalsRegistrationStarted();
    }

标签: mocha.jsassertsoliditytruffle

解决方案


签出这个答案https://ethereum.stackexchange.com/questions/42995/how-to-send-ether-to-a-contract-in-truffle-test我已经设法查找了。这是因为合约功能需要发送以太币,特别是标记为“应付”


推荐阅读