0

In my contract I am use the below mapping. mapping(address => uint)Drivers In the function

    function registerDriver(uint _perkm) public payable returns(string memory) {
           require(Drivers[msg.sender] == 0,"You are already registered as 
    Driver.");
            driverCount++;
            driverList.push(Driver({
                driverID : driverCount,
            driverAddr : msg.sender,
            rideID : 0,
            perkm : _perkm,
            currentStatus : available,
            bids: 0

        }));
        Drivers[msg.sender] = driverCount;
        return "You are registered as Driver.";
    }

Can someone help me write test case for the above function? i.e - test case for my require statement. Thank you very much!

0

The test case for your require statement should be for Drivers[msg.sender] == 0, not for msg.sender == 0 as specified in the title of your question (which by the way is impossible, since you'll need the private key of address 0 in order to generate such transaction).

That said, the test case above is really simple - just call the function twice using the same account.

Side note: the return-value of this function looks completely redundant; I'd advise you to get rid of it.

  • Thanks for responding! So, I actually dont know how to write the test case for this condition.. I can test it by clicking the funciton twice in Remix. how do I write a unit test case say for require condition? Am a newbie here! – Shalu Sep 24 at 16:35
  • @Shalu: You can start here. – goodvibration Sep 24 at 16:52
  • Thank you, I have no pointers for testing msg.sender part only.. there arent any examples for this msg.sender test is it? – Shalu Sep 24 at 17:02
  • @Shalu: Have you read my answer??? There's nothing to test with a specific msg.sender here. Just call the function twice with the same account. Make sure that it completes successfully the first time, and reverts (throws an exception) the second time. – goodvibration Sep 24 at 17:05
  • thank you very much for the confirmation! – Shalu Sep 24 at 17:28
0
require(Driver[msg.sender] == address(0));

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.