7

What is the best way to write contracts in a TDD style? The eos source repo has unit tests which seem to be focused around testing the platform but my question is focused at the next level up (based on the assumption that the platform is tested and working). Therefore I would like to test the contract running on a running testnest (local for now).

My solution so far has been to use custom bash scripts calling the cleos command with manual assertions like this but now I'm exploring options such as rspec with rspec-command so the assertions can be automated and the output more readable.

  • good job man! I'm also curious to know how to do that – Leo Ribeiro May 11 '18 at 15:04
2

This seemed like an important thing to be able to answer since good automated tests are part of completing a piece of code IMHO, particularly a smart contract dealing with lots of valuable assets. Through experimenting I tried BATS which showed promise due to the lack of yet another dependency but I found it difficult to make assertions for JSON results and it was giving some false positives. The test reporting and layout was also limited.

So I explored my original thought of rspec with rspec-command. Although this requires more dependencies and a different language to write the tests, the layout and output is simple to read and the tests easy to write in ruby.

describe "Issue new currency" do
  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
end

Here's a more complete example. Hope it helps someone:

https://gist.github.com/dallasjohnson/6ef228ee38292285f020a9db245e246d

Not the answer you're looking for? Browse other questions tagged or ask your own question.