Coder Social home page Coder Social logo

sidsethi / openzeppelin-test-helpers Goto Github PK

View Code? Open in Web Editor NEW

This project forked from openzeppelin/openzeppelin-test-helpers

0.0 0.0 0.0 125 KB

JavaScript testing helpers for Ethereum smart contract development.

License: MIT License

Solidity 11.13% JavaScript 83.91% Shell 4.97%

openzeppelin-test-helpers's Introduction

OpenZeppelin test helpers

NPM Package Build Status

JavaScript testing helpers for Ethereum smart contract development. These are specially suited for truffle 5 (using web3 1.0). chai bn.js assertions using chai-bn are also included.

Installation

npm install --save-dev openzeppelin-test-helpers

Usage

const { BN, constants, expectEvent, shouldFail } = require('openzeppelin-test-helpers');

const ERC20 = artifacts.require('ERC20');

contract('ERC20', ([sender, receiver]) => {
  beforeEach(async function () {
    this.erc20 = ERC20.new();
    this.value = new BN(1);
  });

  it('reverts when transferring tokens to the zero address', async function () {
    await shouldFail.reverting(this.erc20.transfer(constants.ZERO_ADDRESS, this.value, { from: sender }));
  });

  it('emits a Transfer event on successful transfers', async function () {
    const { logs } = this.erc20.transfer(receiver, this.value, { from: sender });
    expectEvent.inLogs(logs, 'Transfer', { from: sender, to: receiver, value: this.value });
  });

  it('updates balances on successful transfers', async function () {
    this.erc20.transfer(receiver, this.value, { from: sender });
    (await this.token.balanceOf(receiver)).should.be.bignumber.equal(this.value);
  });
});

Reference

This documentation is a work in progress: if in doubt, head over to the tests directory to see examples of how each helper can be used.

All returned numbers are of type BN.


balance

async balance.current (account)

Returns the current Ether balance of an account.

async balance.difference (account, promiseFunc)

Returns the change in the Ether balance of an account caused by executing promiseFunc (which will be awaited on).

(await balance.difference(receiver, () =>
  send.ether(sender, receiver, ether('1')))
).should.be.bignumber.equal(ether('1'));

BN

A bn.js object. Use new BN(number) to create BN instances.


ether

Converts a value in Ether to wei.


expect

A chai expect instance, containing the bignumber property (via chai-bn).

expect(new BN('2')).to.be.bignumber.equal('2');

expectEvent

inLogs (logs, eventName, eventArgs = {})

Asserts logs contains an entry for an event with name eventName, for which all entries in eventArgs match.

async function inConstruction (contract, eventName, eventArgs = {})

Same as inLogs, but for events emitted during the construction of contract.

const contract = await MyContract.new(5);
await expectEvent.inConstruction(contract, 'Created', { value: 5 });

async inTransaction (txHash, emitter, eventName, eventArgs = {})

Same as inLogs, but for events emitted in an arbitrary transaction (of hash txHash), by an arbitrary contract (emitter), even if it was indirectly called (i.e. if it was called by another smart contract and not an externally owned account).


makeInterfaceId (interfaces = [])

Calculates the EIP 165 interface ID of a contract, given a series of function signatures.


send

async send.ether (from, to, value)

Sends value Ether from from to to.

async function send.transaction (target, name, argsTypes, argsValues, opts = {})

Sends a transaction to contract target, calling method name with argValues, which are of type argTypes (as per the method's signature).


should

A chai should instance, containing the bignumber property (via chai-bn).


shouldFail

Collection of assertions for failures (similar to chai's throw). shouldFail will accept any exception type, but more specific functions exist and their usage is encouraged.

async shouldFail.reverting (promise)

Only accepts failures caused due to an EVM revert (e.g. a failed require).

async shouldFail.reverting.withMessage (promise, message)

Like shouldFail.reverting, this helper only accepts failures caused due to an EVM revert (e.g. a failed require). Furthermore, it checks whether revert reason string includes passed message. For example:

contract Owned {
    address private _owner;

    constructor () {
        _owner = msg.sender;
    }

    function doOwnerOperation() public view {
        require(msg.sender == _owner, "Unauthorized");
        ....
    }
}

Can be tested as follows:

const { shouldFail } = require('openzeppelin-test-helpers');

const Owned = artifacts.require('Owned');

contract('Owned', ([owner, other]) => {
  beforeEach(async function () {
    this.owned = Owned.new();
  });

  describe('doOwnerOperation', function() {
    it('Fails when called by a non-owner account', async function () {
      await shouldFail.reverting.withMessage(this.owned.doOwnerOperation({ from: other }), "Unauthorized");
    });
  });
  ...

Use this helper to specify the expected error message, when you're testing a function that can revert for multiple reasons.

async shouldFail.throwing (promise)

Only accepts failures due to a failed assert (which executes an invalid opcode).

async shouldFail.outOfGas (promise)

Only accepts failures due to the transaction running out of gas.


time

async time.advanceBlock()

Forces a block to be mined, incrementing the block height.

async time.latest()

Returns the timestamp of the latest mined block. Should be coupled with advanceBlock to retrieve the current blockchain time.

async time.latestBlock()

Returns the latest mined block number.

async time.increase(duration)

Increases the time of the blockchain by duration (in seconds), and mines a new block with that timestamp.

async time.increaseTo(target)

Same as increase, but a target time is specified instead of a duration.

async time.duration

Helpers to convert different time units to seconds. Available helpers are: seconds, minutes, hours, days, weeks and years.

await time.increase(time.years(2));

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.