繁体   English   中英

Solidity 单元测试未使用正确的发件人来调用被测 function

[英]Solidity unit testing isn't using the correct sender to call the function under test

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#customization

当您说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');
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM