1

This is my code, a simple lottery contract, saves the address participants in the mapping, it is not finished but i want to test if i can receive the payment

pragma solidity ^0.4.23;

contract Lottery {

  uint lotteryFee = 0.1 ether;
  uint8 counter = 0;
  mapping (uint8 => address) participants;

  function participate() external payable returns(address) {
    require(msg.value == lotteryFee);
    counter = counter + 1;
    participants[counter] = msg.sender;
    return(participants[counter]);
  }

  function getBalance() public returns (uint) { 
    uint contractBalance = address(this).balance;
    return(contractBalance);
  }
}

and my test is this:

pragma solidity ^0.4.23;

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

contract TestLottery1 {
  Lottery lottery = Lottery(DeployedAddresses.Lottery());
  //Testing the participate() function
  function testUserCanParticipate() public {
    address returnedId = lottery.participate();
    address expected = msg.sender;
    Assert.equal(returnedId, expected, "You are participating");
  }
}

how or where i put the value i want to send to the contract?

1

You need to specify the initial balance first in your testing contract and the send the value to the function

contract TestLottery1 {
  uint public initialBalance = 1 ether;
  Lottery lottery = Lottery(DeployedAddresses.Lottery());

  //Testing the participate() function
  function testUserCanParticipate() public {
    address returnedId = lottery.participate.value(1)(); //here the value is in wei
    address expected = msg.sender;
    Assert.equal(returnedId, expected, "You are participating");
  }
}

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.