-
-
Save johnteee/39ae5def56a96c89fe96fc9834e508d2 to your computer and use it in GitHub Desktop.
Unit Test for EOS.IO contracts using rspec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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