【问题标题】:Truffle and Ganache-cli test case failsTruffle 和 Ganache-cli 测试用例失败
【发布时间】:2019-05-21 16:45:41
【问题描述】:

所以我有这个设置:truffleganache-cli

我正在向我的合约发送一些以太币,这是我的合约的相关部分:

    mapping(address => uint256) public balanceOf;

    function () payable public {
              uint amount = msg.value;
              balanceOf[msg.sender] += amount;
        }

在松露中,这就是我发送以太币的方式。

it("Test if can be payed", function(){
    return web3.eth.sendTransaction({
           from:fromAddr, 
           to:MyContract.address,  
           value:amountToSend
    }).then(function(res){  
           expect(res).to.not.be.an("error"); // test passed
    });
 });

it("Test if contract received ether", function(){
        return web3.eth.getBalance(MyContract.address, 
               function(err, res){
                        expect(parseInt(res)).to.be.at.least(1000000000000000000); // test passed
                });
});

it("Catch if balanceOf "+fromAddr, function(){
        return sale.balanceOf.call(fromAddr).then(function(res){
                        expect(parseInt(res)).to.be.at.least(1); // fails the test
               });
});

我做得对吗?测试失败的原因可能是什么? 松露测试输出:

AssertionError: expected 0 to be at least 1
      + expected - actual

      -0
      +1

如果需要,我可以提供更多信息。

更新: 澄清sale 是全局变量。

   it("Test if MyContract is deployed", function(){
            return MyContract.deployed().then(function(instance){
                   sale = instance;
            });
   });

【问题讨论】:

    标签: solidity web3 truffle ganache


    【解决方案1】:

    我想这就是你要找的:

    文件路径: test/vault.js

    const Vault = artifacts.require("Vault");
    
    contract("Vault test", async accounts => {
    
        // Rely on one instance for all tests.
        let vault;
        let fromAccount   = accounts[0];
        let oneEtherInWei = web3.utils.toWei('1', 'ether');
    
        // Runs before all tests.
        // https://mochajs.org/#hooks
        before(async () => {
            vault = await Vault.deployed();
        });
    
        // The `receipt` will return boolean.
        // https://web3js.readthedocs.io/en/1.0/web3-eth.html#gettransactionreceipt
        it("Test if 1 ether can be paid", async () => {
            let receipt = await web3.eth.sendTransaction({
                from:  fromAccount, 
                to:    vault.address, 
                value: oneEtherInWei
            });
            expect(receipt.status).to.equal(true);
        });
    
        it("Test if contract received 1 ether", async () => {
            let balance = await web3.eth.getBalance(vault.address);
            expect(balance).to.equal(oneEtherInWei);
        });
    
        // In Web3JS v1.0, `fromWei` will return string.
        // In order to use `at.least`, string needs to be parsed to integer.
        it("Test if balanceOf fromAccount is at least 1 ether in the contract", async () => {
            let balanceOf    = await vault.balanceOf.call(fromAccount);
            let balanceOfInt = parseInt(web3.utils.fromWei(balanceOf, 'ether'));
            expect(balanceOfInt).to.be.at.least(1);
        });
    });
    

    你可以看到完整的项目here。请注意,我使用的是Truffle v5Ganache v2。请参阅 GitLab 存储库中的 README 文件。

    回到你的问题,有两个错误:

    1. sale 未定义。我感觉你实际上指的是MyContract

    2. 为了在 ChaiJS 中使用 least 方法,您需要确保您传递的是整数。 balanceOf 调用返回 BigNumberBN 对象,您不能将其与 .least 方法一起使用。

    仅供参考,Truffle v5 现在默认使用BN(以前为BigNumber)。更多信息here

    如果这有帮助,请告诉我。

    【讨论】:

    • 我添加了一些代码来澄清sale。关于第2点是我parseInt(res)的方式不正确吗?我也有疑问,如果比使用 `.then' 更可取,是否使用 async/await
    • 我试过按照你的回答,但出现完全相同的失败,根据你的回答这将通过:it("Test if contract received 1 ether", async () => {这将失败:it("Test if balanceOf fromAccount is at least 1 ether in the contract", async () => {
    • async/await 更干净,会让你的代码整洁。尝试使用像我这样的环境,看看它是否有效。检查 repo 中的 README 文件。
    • 谢谢你的帮助,我周末加班看看。
    猜你喜欢
    • 1970-01-01
    • 2015-11-16
    • 2011-03-29
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 2022-08-22
    相关资源
    最近更新 更多