Giter Site home page Giter Site logo

v9hstk / 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 1.01 MB

Assertion library for Ethereum smart contract testing

Home Page: https://docs.openzeppelin.com/test-helpers

License: MIT License

JavaScript 89.54% Solidity 5.85% Shell 4.61%

openzeppelin-test-helpers's Introduction

OpenZeppelin Test Helpers

Docs NPM Package Build Status

Assertion library for Ethereum smart contract testing. Make sure your contracts behave as expected.

Test Helpers integrates seamlessly with OpenZeppelin Test Environment, but it also supports Truffle tests and regular web3 workflows.

Overview

Installation

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

Usage

Import @openzeppelin/test-helpers in your test files to access the different assertions and utilities.

Note: The following snippet uses OpenZeppelin Test Environment: a Truffle-based setup would work the same way.

const { accounts, contract } = require('@openzeppelin/test-environment');

const {
  BN,           // Big Number support
  constants,    // Common constants, like the zero address and largest integers
  expectEvent,  // Assertions for emitted events
  expectRevert, // Assertions for transactions that should fail
} = require('@openzeppelin/test-helpers');

const ERC20 = contract.fromArtifacts('ERC20');

describe('ERC20', function () {
  const [sender, receiver] =  accounts;

  beforeEach(async function () {
    // The bundled BN library is the same one web3 uses under the hood
    this.value = new BN(1);

    this.erc20 = await ERC20.new();
  });

  it('reverts when transferring tokens to the zero address', async function () {
    // Conditions that trigger a require statement can be precisely tested
    await expectRevert(
      this.erc20.transfer(constants.ZERO_ADDRESS, this.value, { from: sender }),
      'ERC20: transfer to the zero address',
    );
  });

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

    // Event assertions can verify that the arguments are the expected ones
    expectEvent(receipt, 'Transfer', {
      from: sender,
      to: receiver,
      value: this.value,
    });
  });

  it('updates balances on successful transfers', async function () {
    this.erc20.transfer(receiver, this.value, { from: sender });

    // BN assertions are automatically available via chai-bn (if using Chai)
    expect(await this.erc20.balanceOf(receiver))
      .to.be.bignumber.equal(this.value);
  });
});

Learn More

License

MIT

openzeppelin-test-helpers's People

Contributors

nventuro avatar frangio avatar dependabot[bot] avatar forshtat avatar spalladino avatar naiba avatar remon-nashid avatar jgcarv avatar aniket-engg avatar abcoathup avatar cgewecke avatar bruce-plutusds avatar scottt avatar 3esmit avatar pmizel avatar alcuadrado avatar gorgos avatar dotrungkien avatar kingdido999 avatar djuanit0x avatar chrisb87 avatar changhwan0813 avatar alejoamiras avatar auhau avatar

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.