I am having trouble testing a very naive transfer method for my coin, based off of the ethereum.org demo coin. I've simplified it down into the essentials, yet my solidity unit test keeps failing. Here's the coin contract:

pragma solidity ^0.4.24;

// A very naive coin for sanity checking                                                                                                                                                              
contract Mikancoin {
  uint public totalSupply;
  uint8 public decimals = 3;
  mapping (address => uint) public balanceOf;

  constructor(uint _initialSupply) public {
    totalSupply = _initialSupply * 10 ** uint(decimals);
    balanceOf[msg.sender] = totalSupply;

  function transfer(address _to, uint _tokens) public returns (bool success) {
    // todo: add more checks                                                                                                                                                                               
    balanceOf[msg.sender] -= _tokens;
    balanceOf[_to] += _tokens;
    return true;

  function balanceOf(address _tokenOwner) public view returns (uint balance) {
    return balanceOf[_tokenOwner];

Now I wrote a test of the transfer method:

pragma solidity ^0.4.24;

import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/Mikancoin.sol";

contract TestMikancoin {
  address fox = 0x284A84baA00626e2773a1138D53923b4acAED2F4;

  function testTransfer() public {
    Mikancoin mikan = Mikancoin(DeployedAddresses.Mikancoin());

    uint startingBalance = mikan.balanceOf(msg.sender);

    Assert.equal(mikan.balanceOf(fox), 0, "Initial fox balance");

    uint tokens = 5;
    Assert.isTrue(mikan.transfer(fox, tokens), "Transfer succeeded");
    Assert.equal(mikan.balanceOf(fox), tokens, "Fox balance after transfer");
    Assert.equal(mikan.balanceOf(msg.sender), startingBalance - tokens, "Sender balance after transfer");

I'm running locally against ganache-cli via truffle test. It fails with the following output:

1 failing

1) TestMikancoin testTransfer: Error: Sender balance after transfer (Tested: 99000, Against: 98995)

1 Answer 1


I figured it out. I need to use this instead of msg.sender.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.