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:
-
contract
to access contract -
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
- Install ganache-cli
npm install -g ganache-cli
brew install solidity
- Add gem to a Gemfile Add this line to your application's Gemfile:
gem 'rspec-eth'
- Require extension in
spec_helper.rb
orrails_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.