我的合同
pragma solidity ^0.8.13;
contract Escrow{
address payable public dev;
address public website;
address public customer;
uint public amount;
constructor( address payable _dev, address _customer,uint _amount){
dev = _dev;
website = msg.sender;
customer = _customer;
amount = _amount;
}
function deposit() payable public{
require(msg.sender == customer, "Only customer can add money");
require(address(this).balance <= amount, "Money can not exceed given amount");
}
function sendMoney() public{
dev.transfer(amount);
}
}部署脚本
const Escrow = artifacts.require("Escrow");
module.exports = function (deployer, network, accounts) {
deployer.deploy(Escrow, accounts[1],accounts[2], 1000);
};当我在混合网站上做手工测试时,它很好用。但是在测试sendMoney函数时,它是抛出错误
const Escrow = artifacts.require("Escrow");
contract("Escrow", (accounts) => {
let escrow = null;
const [website, dev, customer] = accounts;
before(async () => {
escrow = await Escrow.deployed();
});
it("Check money send", async ()=>{
try{
const prevDevBalance = web3.utils.toBN(await web3.eth.getBalance(dev))
await escrow.sendMoney({from:accounts[0]})
const newDevBalance = web3.utils.toBN(await web3.eth.getBalance(dev))
assert(true)
return
}catch(e){
assert(false)
console.log("ERRORR",e)
}
})
});有时测试是成功的,但90%的时间抛出这个问题。
1) Contract: Escrow
Check money send:
Uncaught AssertionError: Unspecified AssertionError
at checkError (test/index.js:7:5)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
UnhandledRejections detected
Promise {
<rejected> AssertionError: Unspecified AssertionError
at checkError (test/index.js:7:5)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
showDiff: false,
actual: null,
expected: undefined,
uncaught: true
}
} AssertionError: Unspecified AssertionError
at checkError (test/index.js:7:5)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
showDiff: false,
actual: null,
expected: undefined,
uncaught: true
}发布于 2022-05-26 13:02:35
您似乎没有初始化契约,它的构造函数需要3个参数address payable _dev, address _customer,uint _amount。
用这个来部署-
before(async () => {
escrow = await Escrow.new(dev, customer, "1");
}); https://ethereum.stackexchange.com/questions/128995
复制相似问题