TypeError:测试固定性合同时无法读取未定义的属性“调用”

时间:2018-08-28 07:32:58

标签: solidity truffle

我有这样的问题。我在观看视频教程时正在使用以太坊构建dapp。

这是我的智能合约。

pragma solidity ^0.4.0;

contract SimpleWallet {
    address owner;
    mapping(address => bool) isAllowedToSendsFundsMapping;

    event deposit(address _sender, uint amount);
    event  withdraw(address _sender, uint amount, address _benificiary);

    function SimpleWallet()public{
        owner= msg.sender;
    }

    function (){
        if(msg.sender == owner || isAllowedToSendsFundsMapping[msg.sender]== true){
            deposit(msg.sender, msg.value);
        }else{
            throw;
        }
    }

    function sendFunds(uint amount, address receiver) returns (uint){
        if(msg.sender == owner || isAllowedToSendsFundsMapping[msg.sender]){
            if(this.balance>= amount){
                if(!receiver.send(amount)){
                    throw;
                }
                withdraw(msg.sender, amount, receiver);
                return this.balance;
            }
        }
    }

    function allowAddressToSendMoney(address _address){
        if(msg.sender == owner){
            isAllowedToSendsFundsMapping[_address]=true;
        }
    }

    function disallowAddressToSendMoney(address _address){
        if(msg.sender == owner){
            isAllowedToSendsFundsMapping[_address]=false;
        }
    }

    function isAllowedToSend(address _address) constant returns (bool){
        return isAllowedToSendsFundsMapping[_address]|| msg.sender == owner;
    }

    function killWallet(){
        if(msg.sender == owner){
            selfdestruct(owner);
        }
    }


}

我在这里提供测试文件。

var SimpleWallet = artifacts.require('./SimpleWallet.sol')
contract('SimpleWallet', function (accounts) {
  it('the owner is allowed to send funds', function () {
    var myContract = SimpleWallet.deployed()
    return myContract.isAllowedToSend.call(accounts[0]).then(function (isAllowed) {
      assert.equals(isAllowed, true, 'the owner should have been allowed to send funds')
    })
  })
})

但是当我在控制台中进行松露测试时,它给了我这样的错误。

 TypeError: Cannot read property 'call' of undefined

我重新检查了带有视频的代码,但是两个代码相似。它在视频中可以正常工作,但是在我的计算机上却无法正常工作。我用谷歌搜索,但是找不到适合我问题的答案。有人可以帮助我解决这个问题吗?谢谢。

1 个答案:

答案 0 :(得分:1)

修正并简化了您的测试文件,到这里:

const SIMPLE_WALLET = artifacts.require('SimpleWallet.sol');

contract('SimpleWallet', function (accounts) {

  let simpleWallet;

  beforeEach('setup contract for each test case', async () => {
    simpleWallet = await SIMPLE_WALLET.new({from: accounts[0]})
  })

  it('should let owner to send funds', async () => {
    const isAllowed = await simpleWallet.isAllowedToSend(accounts[0]);
    assert(isAllowed);
  })
})