0

所以我有这个设置: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;
            });
   });
4

1 回答 1

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);
    });
});

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

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

  1. sale没有定义。我有一种感觉,你实际上指的是MyContract

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

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

让我知道这是否有帮助。

于 2018-12-21T11:51:54.303 回答