Skip to content

RSpec extension that allows to easily test solidity smart contracts

License

Notifications You must be signed in to change notification settings

TheSmartnik/rspec-eth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RSpec::Eth

RSpec extension that allows to easily test solidity smart contracts.

What it does

  • Spins up ganache server for tests
  • Adds a few handy methods to ease testing of solidity contracts in ruby main ones:
    1. contract to access contract
    2. accounts to access addresses used by ganache

It's build on top of etherium.rb. For documentation on how to interact with etherium blockchain please refer to its documentation

Example Usage

Given a simple Greeter contract. Here is an example of basic spec

RSpec.describe 'SimpleGreeter', type: :smart_contract do
  before { contract.deploy_and_wait }

  it 'sets greeting' do
    expect(contract.call.greet).to eq("Hello, World!")
  end

  it 'changes message' do
    contract.transact_and_wait.set_super_greeting("Yo")

    expect(contract.call.greet).to eq("Yo")
  end

  context 'when sender not owner' do
    before { contract.sender = accounts[1] }

    it 'trying to set not from owner' do
      expect {
        contract.transact_and_wait.set_super_greeting("Yo")
      }.to raise_exception(IOError, "VM Exception while processing transaction: revert Only owner")
    end
  end
end

Installation

Prerequisite

  1. Install ganache-cli
npm install -g ganache-cli
  1. Install solidity compilier
brew install solidity
  1. Add gem to a Gemfile Add this line to your application's Gemfile:
gem 'rspec-eth'
  1. Require extension in spec_helper.rb or rails_helper.rb
require 'rspec/eth'

Configuration

RSpec::Eth provides a few configuration option that you probably won't need

# spec_helper.rb

RSpec::Eth.configure do |config|
  config.account_keys_path = temp_path # Path for accounts created
  config.host = '127.0.0.1' # Host of ganache server
  config.port = '8545' # Port of ganache server
  config.contracts_path = 'contracts'# Set paths for your contracts
end

Development

After checking out the repo, run bundle install to install dependencies. Then, run bundle exec rspec to run the tests.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/TheSmartnik/rspec-eth.

About

RSpec extension that allows to easily test solidity smart contracts

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published