0

my solidity MyContract running on testrpc with truffle

i am trying to write a test using solidity to test the crowdsale contract i want to test when an address send 1 eth to the contract address the contract produce the right tokens number that's how i went about it

MyCrowdSale.sol

function () payable { 
    // codes....
}

TestMyCrowdSale.sol

pragma solidity ^0.4.11;

import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/MyToken.sol";
import "../contracts/MyCrowdsale.sol";

contract TestMyCrowdsale {

    function testCrowdsaleTransfer() {
        address _address1 = 0x9facfb4743b844222ba4a5ef5b7ef46ce916716e;
        address _address2 = 0x996b2237118af309c74adc16665f29f60bcb7437;

        MyToken coin = new MyToken();
        MyCrowdsale crowdsale = new MyCrowdsale(DeployedAddresses.MyToken(), _address1, _address1, 1504423393);

        // Send 1 ether to the crowdsale contract Address
        _address2.transfer(DeployedAddresses.MyCrowdsale(), 1 ether);

        Assert.equal(coin.balanceOf(_address2), 150000000000, "_address2 should have 150000000000 Token");
        Assert.equal(crowdsale.balanceOf(_address2), 150000000000, "_address2 should have 150000000000 Token");

    }

}
0

according to truffle docs you can send ether by setting initialBalance

[TESTING ETHER TRANSACTIONS][1]

You can also test how your contracts react to receiving Ether, and script that interaction within Solidity. To do so, your Solidity test should have a public function that returns a uint, called initialBalance. This can be written directly as a function or a public variable, as shown below. When your test contract is deployed to the network, Truffle will send that amount of Ether from your test account to your test contract. Your test contract can then use that Ether to script Ether interactions within your contract under test. Note that initialBalance is optional and not required.

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

contract TestContract {
  // Truffle will send the TestContract one Ether after deploying the contract.
  uint public initialBalance = 1 ether;

  function testInitialBalanceUsingDeployedContract() {
    MyContract myContract = MyContract(DeployedAddresses.MyContract());

    // perform an action which sends value to myContract, then assert.
    myContract.send(...);
  }

  function () {
    // This will NOT be executed when Ether is sent. \o/
  }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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