Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unit Test for EOS.IO contracts using rspec
require 'rspec_command'
require "json"
# 1. A recent version of Ruby is required
# 2. Ensure the required gems are installed with `gem install rspec json rspec-command`
# 3. Run this from the command line with rspec sampletokenSpec.rb
# Optionally output the test results with -f [p|d|h] for required views of the test results.
# For debugging I added a clear action to the contract which clears everything in the tables
# for a "clean" contract environment but this should not be shipped with the production code.
RSpec.configure do |config|
config.include RSpecCommand
end
describe "Clear all tables first (only for debugging and testing)" do
command %(cleos push action sampletoken clear '{ "sym": "1.0 ABC", "owner": "sampletoken"}' -p eosio), allow_error: true
its(:stdout) { is_expected.to include('sampletoken::clear') }
end
describe "Create a new currency" do
context "without account auth should fail" do
command %(cleos push action sampletoken create '{ "issuer": "sampletoken", "maximum_supply": "10000.0000 ABC", "can_freeze": 0, "can_recall": 0, "can_whitelist": 0}'), allow_error: true
its(:stderr) { is_expected.to include('transaction must have at least one authorization') }
end
context "with mismatching auth should fail" do
command %(cleos push action sampletoken create '{ "issuer": "sampletoken", "maximum_supply": "10000.0000 ABC", "can_freeze": 0, "can_recall": 0, "can_whitelist": 0}' -p eosio), allow_error: true
its(:stderr) { is_expected.to include('missing authority of sampletoken') }
end
end
describe "Issue new currency" do
context "without valid auth should fail" do
command %(cleos push action sampletoken issue '{ "to": "sampletoken", "quantity": "1000.0000 ABC", "memo": "Initial amount of tokens for you."}'), allow_error: true
its(:stderr) { is_expected.to include('transaction must have at least one authorization') }
end
context "with valid auth should succeed" do
command %(cleos push action sampletoken issue '{ "to": "sampletoken", "quantity": "1000.0000 ABC", "memo": "Initial amount of tokens for you."}' -p sampletoken), allow_error: true
its(:stdout) { is_expected.to include('sampletoken::issue') }
end
context "greater than max should fail" do
command %(cleos push action sampletoken issue '{ "to": "sampletoken", "quantity": "11000.0000 ABC", "memo": "Initial amount of tokens for you."}' -p sampletoken), allow_error: true
its(:stderr) { is_expected.to include('quantity exceeds available supply') }
end
end
describe "Read back the stats after issuing currency" do
command %(cleos get currency stats sampletoken ABC), allow_error: true
it "should display max supply, supply and issuer" do
expect(JSON.parse(subject.stdout)).to eq JSON.parse <<~JSON
{
"ABC": {
"supply": "3000.0000 ABC",
"max_supply": "10000.0000 ABC",
"issuer": "sampletoken"
}
}
JSON
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.