Why does using {value: 1} in a test results in an exception with no text?

by Danny Tuppeny   Last Updated May 24, 2020 18:28 PM - source

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).



Answers 1


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

goodvibration
goodvibration
May 24, 2020 17:59 PM

Related Questions



Call functions in Truffle as different account

Updated July 24, 2017 11:28 AM

difference between describe and context

Updated October 25, 2018 08:28 AM

How to Unit test modifiers in Solidity

Updated December 12, 2017 12:28 PM

How to run a test file (.go) from ethereum source?

Updated September 18, 2018 11:28 AM