--- tags: ncd title: NEAR Certified Developer -3- Day 3 description: This course is an opportunity for web developers to earn a Certificate of Completion that represents the ability to design, develop, test and deploy smart contracts on the NEAR platform. image: https://near.org/wp-content/themes/near-19/assets/downloads/near_logo.svg GA: UA-188414449-3 --- # Day 3 ## Testing web 3 *Learn to write unit and simulation tests for smart contracts.* --- ### :sparkles: The goal for today is to write tests that pass for your contracts. :sparkles: --- :::success ### :green_book: CORE Activities ::: 1. In the [Resources section](#-Resources) below choose AssemblyScript or Rust - **WRITE** 3-5 unit tests for **each** contract listed as **CORE Activity** :::info ### :blue_book: 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 - [Scavenger Hunt Challenge #4](https://hackmd.io/@nearly-learning/hunt-04) 4. Start from a blank document (or just boilerplate) and try to test-drive (TDD) a contract. :::warning ### :orange_book: 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. - [NEARly Neighbors](https://learn-near.github.io/nearly-neighbors) has simulation tests - [Workshop on Cross-contract Calls](http://bit.ly/near-xcc) has simulation tests --- > _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](https://dergigi.com/2021/01/14/bitcoin-is-time/)* --- # :dart: 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 ]**](https://airtable.com/shrG4kGx80F55usI4) 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](https://github.com/jtenner/as-pect) and the syntax resembles RSpec. The library is well [documented](https://tenner-joshua.gitbook.io/as-pect/) but sometimes the [tests for the testing library](https://github.com/jtenner/as-pect/tree/master/packages/assembly/assembly/__tests__) might be the best source of examples to help you learn quickly. Almost all of the examples available on [near.dev](http://near.dev) include unit tests. ![The list of AssemblyScript contracts](https://i.imgur.com/RJN8AkD.png) ## Rust If you intend to focus on Rust, [**[ OPEN the list of Rust contracts ]**](https://airtable.com/shrckdZAMgjbP3uBC) 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](https://doc.rust-lang.org/rust-by-example/testing/unit_testing.html) or ["the book"](https://doc.rust-lang.org/book/ch11-01-writing-tests.html). All of NEAR's [Core Contracts](https://github.com/near/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`](https://github.com/near/core-contracts/blob/master/whitelist/src/tests/test_utils.rs#L21) import that sets up the MockedBlockchain context for unit tests in a separate file ![The list of Rust contracts](https://i.imgur.com/SGqQP01.png)