1

I'm completely new to smart contracts in Etherum. Using Remix I created a small contract that does nothing except require msg.value == 1:

pragma solidity >=0.4.22 <0.7.0;


contract Danny {
    function foo() external payable {
        require(msg.value == 1, "Must send exactly 1");
    }
}

Then I created a test that just calls it and asserts a nicer message if it fails (I'm not sure why, but by default the error shown by the test framework doesn't include the string from the error):

pragma solidity >=0.4.22 <0.7.0;
import "remix_tests.sol";
import "./danny.sol";


contract DannyTest {
    Danny danny;

    function beforeEach() public {
        danny = new Danny();
    }

    function test1() public {
        try danny.foo() {
        } catch Error(string memory reason) {
            Assert.ok(false, string(reason));
        } catch (bytes memory data) {
            Assert.ok(false, string(data));
        }
    }
}

As expected, this test fails with "Must send exactly 1". So, I then tried to send a value of 1 with the test, using the details from "Testing a method involving msg.value":

try danny.foo{value: 1}()

However this fails with an empty error:

failure

I've tried all sorts of combinations all evening, but haven't been able to make this work. The version of the compiler shown in Remix is 0.6.8 (though I'm not sure if the unit testing plugin uses the one selected here of its own).

1 Answer 1

1

Because your DannyTest instance does not have enough funds to pass that 1 wei.

5
  • Wow, that was the quickest V in history! Literally 1 second after I had posted the answer. Commented May 24, 2020 at 17:59
  • I just came back to post that =) After copying the example and then stripping it down to mine, I realised that the test (which just seems to be another contract) needed the value to pass through. Thanks! Commented May 24, 2020 at 18:00
  • @DannyTuppeny: Yeah, you understand what you gotta in order to solve this, right? Add a payable function in that contract as well, and make sure to call it with a sufficient amount of ether (1 wei in your case) before executing the test. Commented May 24, 2020 at 18:02
  • Yeah, I just figured out I was missing that too - that explains why I thought I had tried it with #value: and it still hadn't worked. It's a shame there's no warning if you miss payable but add #value:x, or a better error if you try to pass wei you don't have. Commented May 24, 2020 at 18:05
  • My real test is working as expected now - thanks :-) Commented May 24, 2020 at 18:06

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.