Testing Your Smart Contracts With JavaScript (Including Async/Await Promises)

Show me the green light to move forward
- node@8.5.0
- truffle@^4.0.0
- ethereumjs-testrpc@^4.0.1
mkdir smart-contract-test-example && cd "$_" && truffle init webpack
const FundRaise = artifacts.require('./FundRaise.sol')contract('FundRaise', function () {})
testrpc
truffle test test/fundRaise.js
Error: Could not find artifacts for ./FundRaise.sol from any sources
at Resolver.require (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:49072:9)
at TestResolver.require (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:89733:30)
...
pragma solidity ^0.4.17;contract FundRaise {}
 0 passing (1ms)
  • it has an owner
  • it accepts funds
  • it is able to pause and un-pause the fundraising activity
  • contract owner is able to remove funds from the contract to herself at any time.
const FundRaise = artifacts.require('./FundRaise.sol')contract('FundRaise', function ([owner]) {
let fundRaise
beforeEach('setup contract for each test', async function () {
fundRaise = await FundRaise.new(owner)
})
it('has an owner', async function () {
assert.equal(await fundRaise.owner(), owner)
})
})
// Allows us to use ES6 in our migrations and tests.
require('babel-register')
require('babel-polyfill')
module.exports = {
networks: {
development: {
host: 'localhost',
port: 8545,
network_id: '*' // Match any network id
}
}
}
...
1 failing
1) Contract: FundRaise has an owner:
AssertionError: expected undefined to equal '0x676c48fb3979cf2e47300e8ce80a99087589650d'
...
pragma solidity ^0.4.17;contract FundRaise {
address public owner;
// @dev constructor function. Sets contract owner
function FundRaise() {
owner = msg.sender;
}
}
Contract: FundRaise
✓ has an owner (41ms)
1 passing (138ms)
const FundRaise = artifacts.require('./FundRaise.sol')  contract('FundRaise', function ([owner, donor]) {
let fundRaise
beforeEach('setup contract for each test', async function () {
fundRaise = await FundRaise.new(owner)
})
it('has an owner', async function () {
assert.equal(await fundRaise.owner(), owner)
})
it('is able to accept funds', async function () {
await fundRaise.sendTransaction({ value: 1e+18, from: donor })
const fundRaiseAddress = await fundRaise.address
assert.equal(web3.eth.getBalance(fundRaiseAddress).toNumber(), 1e+18)
})
})
1 failing1) Contract: FundRaise is able to accept funds:
Error: VM Exception while processing transaction: invalid opcode
pragma solidity ^0.4.17;contract FundRaise {
address public owner;
// @dev constructor function. Sets contract owner
function FundRaise() {
owner = msg.sender;
}

// fallback function that allows contract to accept ETH
function () payable {}
}
Contract: FundRaise
✓ has an owner (38ms)
✓ is able to accept funds (234ms)
2 passing (473ms)
Note the tests for the pause/unpause and owner receive funds tests
Contract: FundRaise
✓ has an owner (46ms)
✓ accepts funds (193ms)
✓ is able to pause and unpause fund activity (436ms)
✓ permits owner to remove funds (653ms)
4 passing (2s)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gustavo (Gus) Guimaraes

Gustavo (Gus) Guimaraes

A curious mind, joie de vivre practitioner