Try   HackMD

Day 3

Testing web 3

Learn to write unit and simulation tests for smart contracts.


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
The goal for today is to write tests that pass for your contracts.
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
CORE Activities

  1. In the Resources section below choose AssemblyScript or Rust

    • WRITE 3-5 unit tests for each contract listed as CORE Activity

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
BONUS Activities

If you have the time to look around the corner, here's a little more for you.

If you want a few more ideas for this challenge, consider the following:

  1. Find a contract that has no unit tests (there are many) and write them

  2. Remove some (or all) of the existing unit tests from a contract and rewrite them.

  3. Check out this little puzzle, it may be fun for you

  4. Start from a blank document (or just boilerplate) and try to test-drive (TDD) a contract.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Going Deeper

If you're feeling fearless, here's about as far as you might take this road in a day

While simulation tests are an evolving work in progress, we do have a few examples which can help you. The key to understanding simulation tests on NEAR is that you're testing the Wasm binary. This means the same simulation test configuration will work for Rust and AssemblyScript.

In fact, the exact same simulation tests can be used against a compiled Rust or compiled AssemblyScript contract. Maybe this is obvious once you consider that simulation tests are using the same on-chain virtual machine so everything you do in a simulation test should be 1:1 repeatable on-chain.

Heads up, simulation tests must be written in Rust. The examples below will make that clear.


What is a new idea — what Satoshi figured out — is how to independently agree upon a history of events without central coordination. He found a way to implement a decentralised timestamping scheme that:

  • a) doesn’t require a time-stamping company or server,
  • b) doesn’t require a newspaper or any other physical medium as proof, and
  • c) can keep the ticks more-or-less constant, even when operating in an environment of ever-faster CPU clock times.

Gigi in Bitcoin is Time


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Resources

REMEMBER: For today’s activity

  • You DO need to build (and run tests, if available) for each contract
  • You should TRY to understand EVERY line of code in the contract

AssemblyScript

If you intend to focus on AssemblyScript, [ OPEN the list of AssemblyScript contracts ]

For a minimum of 3 contracts marked as CORE Activity

  • a) Write 3-5 new unit tests for each contract whether or not it already has unit tests
  • b) Verify the tests pass as expected (tests can be run via command line)

Unit testing is provided by as-pect and the syntax resembles RSpec. The library is well documented but sometimes the tests for the testing library might be the best source of examples to help you learn quickly.

Almost all of the examples available on near.dev include unit tests.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Rust

If you intend to focus on Rust, [ OPEN the list of Rust contracts ]

For a minimum of 3 contracts marked as CORE Activity

  • a) Write 3-5 new unit tests for each contract whether or not it already has unit tests
  • b) Verify the tests pass as expected (tests can be run via IDE or command line)

Unit testing is included as part of the Rust language.

You can read more about unit tests in Rust by Example or "the book".

All of NEAR's Core Contracts include unit tests if you want to see some good examples. You will notice the whitelist contract, for example, includes a test_utils import that sets up the MockedBlockchain context for unit tests in a separate file

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →