pragma solidity 0.6.12;
// This import is automatically injected by Remix
import "remix_tests.sol";
import "remix_accounts.sol";
import "./LottoMock.sol";
... other test cases
contract lottoMultipleEntranceTest {
LottoMock lotto;
/// #sender: account-0
/// #value: 500000000000000
function beforeEach() public payable {
lotto = new LottoMock();
Assert.equal(lotto.getQuantityOfEntrants(), uint256(0), "expecting 0 entrants before entering");
Assert.equal(lotto.getLotteryBalance(), uint256(0), "expecting 0 lottery balance before entering");
Assert.equal(msg.sender, TestsAccounts.getAccount(0), "Invalid sender");
lotto.enter{value:500000000000000}();
Assert.equal(lotto.getLotteryBalance(), uint256(500000000000000), "expecting lottery balance equal to entrance fee after entering");
Assert.equal(lotto.getQuantityOfEntrants(), uint256(1), "user should have successfully entered the lottery");
}
//TODO: needs debugging
///case 7: multiple entrants
/// #sender: account-1
/// #value: 500000000000000
function enterSuccessfullyMultipleEntrants() public payable {
Assert.equal(lotto.getLotteryBalance(), uint256(500000000000000), "One user has already entered.");
Assert.equal(lotto.getQuantityOfEntrants(), uint256(1), "Expecting an existing entry.");
Assert.equal(msg.sender, TestsAccounts.getAccount(1), "Invalid sender");
//TODO - this is using account-0
try lotto.enterDebug1{value:500000000000000}() {
Assert.ok(false, 'succeed unexpected');
} catch Error(string memory reason) {
Assert.equal(reason, "debug", "debug.");
} catch (bytes memory /*lowLevelData*/) {
Assert.ok(false, 'failed unexpected');
}
Assert.equal(lotto.getLotteryBalance(), uint256(1000000000000000), "expecting lottery balance equal to entrance fee for two users after entering");
Assert.equal(lotto.getQuantityOfEntrants(), uint256(2), "second user should have successfully entered the lottery");
}
}
我遇到的问题是在enterSuccessfullyMultipleEntrants
测试中,即使Assert.equal(msg.sender, TestsAccounts.getAccount(1), "Invalid sender");
工作正常,lotto.enterDebug1{value:500000000000000}()
线仍然被测试帐户0调用,而不是帐户-1。有人能告诉我我哪里做错了吗?
参考:https://remix-ide.readthedocs.io/en/latest/unittesting.html定制
当您说try lotto.enterDebug1{}
时,我们正在标记外部呼叫。在这种情况下,msg.sender
被设置为调用合约的地址(即address(this)
,而不是account-0
)。即使你设置了#sender: account-0
,这似乎也不起作用。您可以通过从被调用的契约返回msg.sender
并将其与address(this)
进行比较来观察这一点。
例如,在我们调用的合同中假设如下:
contract Called{
...
funtion sender() public returns(address){
return msg.sender;
}
}
然后在调用契约中:
Called c = new Called();
/// #sender: account-0
function test() public{
try c.sender() returns (address a){
Assert.equal(a, address(this), 'Should be same');
}