Skip to content

Instantly share code, notes, and snippets.

@jstrassburg
Created March 18, 2021 22:01
Show Gist options
  • Save jstrassburg/d00dd0372256ba6054452e8749d22a20 to your computer and use it in GitHub Desktop.
Save jstrassburg/d00dd0372256ba6054452e8749d22a20 to your computer and use it in GitHub Desktop.
Smart contract payable constructor failing test.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.2;
contract Foo {
uint public value;
constructor() payable {
value = msg.value;
}
}
> truffle test
Using network 'development'.
Compiling your contracts...
===========================
> Compiling .\test\TestFoo.sol
> Artifacts written to C:\Users\jstra\AppData\Local\Temp\test--11228-dvwQ6fhl6MiC
> Compiled successfully using:
- solc: 0.8.2+commit.661d1103.Emscripten.clang
TestFoo
1) testConstructor
> No events were emitted
0 passing (27s)
1 failing
1) TestFoo
testConstructor:
Error: Returned error: VM Exception while processing transaction: revert
at Context.<anonymous> (C:\Users\jstra\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\testing\SolidityTest.js:92:1)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:94:5)
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.2;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/Foo.sol";
contract TestFoo {
function testConstructor() public payable {
uint expectedAmount = 233;
Foo foo = new Foo{value: expectedAmount}();
Assert.equal(foo.value(), expectedAmount, "Wrong amount.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment