Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update escrow-testing/testing.md #94

Merged
merged 1 commit into from
Jul 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions docs/11-escrow-testing/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@ sidebar_position: 1
hide_table_of_contents: true
---

You can now learn to write tests for a smart contract program using the Rust programming language and the `gtest` library. This lesson guides you through testing an escrow smart contract program. It covers initializing the contract, depositing funds from the buyer's account and checking for accurate contract execution and failure cases.
You can now learn to write tests for a smart contract program using the Rust programming language and the `gtest` library.

Let's test our method. We'll first create the `tests` directory and `escrow_test.rs` file:
This lesson guides you through testing an escrow smart contract program, covering the following areas
- Initializing the contract
- Depositing funds from the buyer's account
- Checking for accurate contract execution and failure cases

Let's test our method.

We'll first create the `tests` directory and `escrow_test.rs` file:

```bash
mkdir tests
cd tests
touch escrow_test.rs
```

We'll import necessary structures from the `gtest` library and escrow crate and define constants for the buyer, seller and product price. Then, we'll send an init message using the following code:
We'll import necessary structures from the `gtest` library and escrow crate and define constants for the buyer, seller and product price.

Then, we'll send an init message using the following code:

```rust title="tests/escrow_test.rs"
use escrow_io::{InitEscrow, EscrowAction, EscrowEvent};
Expand Down Expand Up @@ -42,7 +51,7 @@ fn deposit() {
```

Next, we'll send a message from the buyer's account using the
[`Program::send_with_value`](https://docs.gear.rs/gtest/struct.Program.html#method.send_with_value) function instead of [`Program::send`](https://docs.gear.rs/gtest/struct.Program.html#method.send) function since we need to send a message with funds. However, the account balance is zero in the test node, so we need to modify it:
[`Program::send_with_value`](https://docs.gear.rs/gtest/struct.Program.html#method.send_with_value) function instead of [`Program::send`](https://docs.gear.rs/gtest/struct.Program.html#method.send) function since we need to send a message with funds. However, the account balance is zero in the test node, so we'll modify it:

```rust title="tests/escrow_test.rs"
sys.mint_to(BUYER, PRICE);
Expand Down Expand Up @@ -76,7 +85,9 @@ fn init_escrow(sys: &System) {
}
```

To obtain the program within the test function, we can utilize the [`System::get_program`](https://docs.gear.rs/gtest/struct.System.html#method.get_program) function provided by the `gtest` library. As discussed in our first lesson, we initialize our program with the first ID. Hence, the complete code for the deposit test function is as follows:
We can utilize the [`System::get_program`](https://docs.gear.rs/gtest/struct.System.html#method.get_program) function provided by the `gtest` library to obtain the program within the test function.

As discussed in our first lesson, we initialize our program with the first ID. Hence, the complete code for the deposit test function is as follows:

```rust title="tests/escrow_test.rs"
const ESCROW_ID: u64 = 1;
Expand Down Expand Up @@ -154,4 +165,4 @@ fn deposit_failures() {
}
```

Great, we have written half of our program. Now it's time for you to code.
Great, we've written half of our program. Now it's time for you to code.