mocha.js - 松露测试:返回错误:处理事务时出现虚拟机异常:恢复
问题描述
我已经多次看到这个错误。网上没有任何东西可以帮助解决它
我连续多次使用相同的函数 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();
}
解决方案
签出这个答案https://ethereum.stackexchange.com/questions/42995/how-to-send-ether-to-a-contract-in-truffle-test我已经设法查找了。这是因为合约功能需要发送以太币,特别是标记为“应付”
推荐阅读
- flutter - 如何在滑块上方添加刻度和标签?
- openssl - 无法加载动态库 'openssl' - PHP 启动
- node.js - 未捕获(承诺)错误:在 XMLHttpRequest.handleError (xhr.js:99) 的 createError (createError.js:16) 处出现网络错误
- spring-boot - OpenAPI 生成的 Spring Boot 服务器不验证所需的属性
- sql - 如何在标准 sql 中使用 json_extract 来获取值
- react-native - 如何在 React 本机 Drawer.Navigator 中添加注销按钮?
- javascript - 使用 Sheet.js 包导出时,如何将 CSV 中的所有单元格格式化为文本?
- c# - C#弹跳立方体游戏,从中心100px移除立方体
- firebase - React Native Pdf 未显示
- php - 如何在 foreach 循环中调用不同的多语言国家列表?