为什么混音的test#2文件失败了?嗨,我是跟随混音的例子测试,可在:https://remix-ide.readthedocs.io/en/latest/unittesting_examples.html。发送者代码是:
pragma solidity ^0.5.3;
contract sender {
address private owner;
constructor() public {
owner = msg.sender;
}
function updateOwner(address newOwner) public {
require(msg.sender == owner, "only current owner can update owner");
owner = newOwner;
}
function getOwner() public view returns (address) {
return owner;
}
}
// testfile是:
pragma solidity 0.5.3;
// This import is automatically injected by Remix
import "remix_tests.sol";
import "tests/sender.sol";
import "remix_accounts.sol";
// File name has to end with '_test.sol', this file can contain more than one testSuite contracts
contract testSuite is sender{
//sender obj;
address acc0;
address acc1;
address acc2;
function beforeAll() public {
acc0 = TestsAccounts.getAccount(0);
acc1 = TestsAccounts.getAccount(1);
acc2 = TestsAccounts.getAccount(2);
}
function testInitialOwner() public {
Assert.equal(getOwner(), acc0, 'owner should be acc0');
}
function updateOwnerOnce() public {
// check method caller is as expected
Assert.ok(msg.sender == acc0, 'caller should be default account i.e. acc0');
// update owner address to acc1
updateOwner(acc1);
// check if owner is set to expected account
Assert.equal(getOwner(), acc1, 'owner should be updated to acc1');
}
function updateOwnerOnceAgain() public {
// check if caller is custom and is as expected
Assert.ok(msg.sender == acc1, 'caller should be custom account i.e. acc1');//This is failing
}
}
我得到的输出如下:
testSuite (tests/sender_test.sol)
✓ Test initial owner
✓ Update owner once
✘ Update owner once again
Error Message:
"caller should be custom account i.e. acc1"
Assertion:
Expected value should be
'true'
Received value:
false
Skipping the remaining tests of the function.
Result for tests/sender_test.sol
Passing: 2
Failing: 1
Total time: 0.30s
我还附上了这张照片:
谁来指点我,有什么问题吗?
祖尔菲。
发布于 2022-01-07 05:02:43
注释说,我们必须使用与account0无关的自定义,而必须使用account1。
实现这一目标的途径是:
/#发件人:帐户-1
因此,我们将使用前面的声明:
updateOwnerOnceAgain()
祖尔菲。
https://ethereum.stackexchange.com/questions/117594
复制相似问题