如何解决智能合约测试-“未捕获的类型错误:无法读取未定义的属性‘调用’”
我正在尝试测试我编写的 ERC20 合约。当我运行 truffle test 时,它不断返回以下错误:
✓ Should deploy smart contract properly
1) "before each" hook: before test for "should call a function that depends on a linked library"
2) "before each" hook: before test for "should call a function that depends on a linked library"
1 passing (172ms)
2 failing
1) Contract: My
"before each" hook: before test for "should call a function that depends on a linked library":
Uncaught TypeError: Cannot read property 'call' of undefined
at /Users/.../test/myToken.js:8:47
at processticksAndRejections (node:internal/process/task_queues:96:5)
2) Contract: My
"before each" hook: before test for "should call a function that depends on a linked library":
Error: done() called multiple times in hook <Contract: MyToken "before each" hook: before test for "should call a function that depends on a linked library"> of file ...
UnhandledRejections detected
Promise {
<rejected> TypeError: Cannot read property 'call' of undefined
at /Users/.../myToken.js:8:47
at processticksAndRejections (node:internal/process/task_queues:96:5) {
uncaught: true,multiple: [ [Error] ]
}
} TypeError: Cannot read property 'call' of undefined
at /Users/.../myToken.js:8:47
at processticksAndRejections (node:internal/process/task_queues:96:5) {
uncaught: true,multiple: [
Error: done() called multiple times in hook <Contract: MyToken "before each" hook: before test for "should call a function that depends on a linked library"> of file /Users/.../myToken.js
at processticksAndRejections (node:internal/process/task_queues:96:5) {
code: 'ERR_MOCHA_MULTIPLE_DONE',valueType: 'undefined',value: undefined
}
]
}
我不明白发生了什么。我几乎从 tuffle 文档中复制了这段代码,所以一切都应该运行。我错过了什么?
这是我的代码。
const { assert } = require("console");
const MyContract = artifacts.require("My");
contract("MyToken",accounts => {
it("Should deploy smart contract properly",() => {
MyContract.deployed()
.then((instance) => instance.getBalance.call(accounts[0]))
.then((balance) => {
assert.equal(
balance.valueOf(),10000,"10000 wasn't in the first account"
);
});
});
it("should call a function that depends on a linked library",() => {
let my;
let myBalance;
let myEthBalance;
return myBalance.deployed()
.then(instance => {
my = instance;
return my.getBalance.call(accounts[0]);
})
.then(outCoinBalance => {
myBalance = outCoinBalance.toNumber();
return my.getBalanceInEth.call(accounts[0]);
})
.then(outCoinBalanceEth => {
myEthBalance = outCoinBalanceEth.toNumber();
})
.then(() => {
assert.equal(myEthBalance,2 * myBalance,"Library function returned unexpected function,linkage may be broken"
);
});
});
it("should send coin correctly",() => {
let my;
// Get initial balances of first and second account.
const account_one = accounts[0];
const account_two = accounts[1];
let account_one_starting_balance;
let account_two_starting_balance;
let account_one_ending_balance;
let account_two_ending_balance;
const amount = 10;
return MyContract.deployed()
.then(instance => {
my = instance;
return my.getBalance.call(account_one);
})
.then(balance => {
account_one_starting_balance = balance.toNumber();
return my.getBalance.call(account_two);
})
.then(balance => {
account_two_starting_balance = balance.toNumber();
return my.sendCoin(account_two,amount,{ from: account_one });
})
.then(() => my.getBalance.call(account_one))
.then(balance => {
account_one_ending_balance = balance.toNumber();
return my.getBalance.call(account_two);
})
.then(balance => {
account_two_ending_balance = balance.toNumber();
assert.equal(
account_one_ending_balance,account_one_starting_balance - amount,"Amount wasn't correctly taken from the sender"
);
assert.equal(
account_two_ending_balance,account_two_starting_balance + amount,"Amount wasn't correctly sent to the receiver"
);
});
});
});```
I have edited some of the names of the variables and the contract names.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。