Project

rspec-eth

0.0
No release in over a year
RSpec extension that spins up ganache server for tests and adds a few handy methods. Expected to be used with ethereum.rb
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

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.