From fdf8d0b2b13d13a3dc8d0927608f2ec0861b852e Mon Sep 17 00:00:00 2001 From: garikbesson Date: Wed, 25 Feb 2026 12:35:40 +0000 Subject: [PATCH] mirgrate rust examples from workspaces to sandbox --- .../testing/integration-test.md | 107 +++++++++--------- 1 file changed, 51 insertions(+), 56 deletions(-) diff --git a/docs/smart-contracts/testing/integration-test.md b/docs/smart-contracts/testing/integration-test.md index 42e8ae3926c..fa730ca0a47 100644 --- a/docs/smart-contracts/testing/integration-test.md +++ b/docs/smart-contracts/testing/integration-test.md @@ -15,29 +15,36 @@ Moreover, when using the local `sandbox` you gain complete control of the networ 2. Simulate errors on callbacks. 3. Control the time-flow and fast-forward into the future (Rust ready, TS coming soon). -In NEAR, integration tests are implemented using a framework called **Workspaces**. Workspaces comes in two flavors: [🦀 Rust](https://github.com/near/workspaces-rs) and [🌐 Typescript](https://github.com/near/workspaces-js). +In NEAR, integration tests are implemented using a framework called **Sandbox**. Sandbox comes in two flavors: [🦀 Rust](https://github.com/near/near-sandbox-rs) and [🌐 Typescript](https://github.com/near/workspaces-js). All of our [examples](https://github.com/near-examples) come with integration testing. :::note Sandbox Testing -NEAR Workspaces allows you to write tests once, and run them either on `testnet` or a local `Sandbox`. By **default**, Workspaces will start a **sandbox** and run your tests **locally**. Lets dive into the features of our framework and see how they can help you. +NEAR Sandbox allows you to write tests once, and run them either on `testnet` or a local `Sandbox`. By **default**, Sandbox will start a **sandbox** and run your tests **locally**. Lets dive into the features of our framework and see how they can help you. ::: --- ## Create Accounts -### Dev Account +### Account - + + url="https://github.com/near-examples/near-workspaces-examples/blob/workspaces-migration/contract-rs/tests/basics.rs" + start="16" end="31" /> + + +
+ +### Dev Account + + - - - - - + url="https://github.com/near-examples/near-workspaces-examples/blob/workspaces-migration/contract-rs/tests/basics.rs" + start="40" end="57" /> @@ -97,8 +97,8 @@ NEAR Workspaces allows you to write tests once, and run them either on `testnet` + url="https://github.com/near-examples/near-workspaces-examples/blob/workspaces-migration/contract-rs/tests/basics.rs" + start="66" end="89" /> @@ -120,14 +120,14 @@ NEAR Workspaces allows you to write tests once, and run them either on `testnet` + url="https://github.com/near-examples/near-workspaces-examples/blob/workspaces-migration/contract-rs/tests/basics.rs" + start="98" end="102" /> :::tip You don't need to assert compiling process everytime. You can use `?` operator to get the result as `Vec` without dealing with `Result>, Error>` type. That way you can directly use this vector to deploy the wasm file into account. Your test will still fail if compiling process fails. ```rust - let contract_wasm = near_workspaces::compile_project("./").await?; + let contract_wasm_path = cargo_near_build::build_with_cli(Default::default())?; ``` ::: @@ -160,8 +160,8 @@ NEAR Workspaces allows you to write tests once, and run them either on `testnet` + url="https://github.com/near-examples/near-workspaces-examples/blob/workspaces-migration/contract-rs/tests/basics.rs" + start="109" end="116" /> :::tip The same as in the case of compilation wasm from code, you don't need to assert reading file process everytime. You can use `expect` method to get the reading file result as `Vec` and provide error message as a parameter. Your test will still fail if compiling process fails. @@ -200,13 +200,6 @@ NEAR Workspaces allows you to write tests once, and run them either on `testnet` ### Dev Deploy - - - - - + url="https://github.com/near-examples/near-workspaces-examples/blob/workspaces-migration/contract-rs/tests/basics.rs" + start="123" end="159" /> @@ -249,8 +242,8 @@ Show contract's logs. You can use `println` or `dbg!` when you want to see information from your code. + url="https://github.com/near-examples/near-workspaces-examples/blob/workspaces-migration/contract-rs/tests/basics.rs" + start="24" end="31" /> In Rust, the output from your code is captured by default and not displayed in the terminal. In order to see the output, you have to use the `--nocapture` flag @@ -259,10 +252,12 @@ Show contract's logs. If you want to access the contracts logs, you can find them in the `tx_outcome.logs()` Vec. ```rust - let tx_outcome = user_account - .call(contract.id(), "set_greeting") - .args_json(json!({"greeting": "Hello World!"})) - .transact() + let tx_outcome = contract + .call_function("set_greeting", json!({"greeting": "Hello World!"})) + .transaction() + .gas(Gas::from_tgas(100)) + .with_signer(contract.account_id().clone(), signer.clone()) + .send_to(&sandbox_network) .await?; assert!(tx_outcome.is_success()); @@ -300,8 +295,8 @@ Show contract's logs. + url="https://github.com/near-examples/near-workspaces-examples/blob/workspaces-migration/contract-rs/tests/basics.rs" + start="168" end="199" /> @@ -323,8 +318,8 @@ Show contract's logs. + url="https://github.com/near-examples/near-workspaces-examples/blob/workspaces-migration/contract-rs/tests/basics.rs" + start="208" end="257" /> @@ -344,8 +339,8 @@ Show contract's logs. + url="https://github.com/near-examples/near-workspaces-examples/blob/workspaces-migration/contract-rs/tests/basics.rs" + start="266" end="323" /> @@ -370,8 +365,8 @@ You can alter contract code, accounts, and access keys using normal transactions + url="https://github.com/near-examples/near-workspaces-examples/blob/workspaces-migration/contract-rs/tests/basics.rs" + start="332" end="384" /> @@ -396,14 +391,14 @@ This approach is more complex to do and also cannot be performed without restart ## Time Traveling -`workspaces` offers support for forwarding the state of the blockchain to the future. This means contracts which require time sensitive data do not need to sit and wait the same amount of time for blocks on the sandbox to be produced. We can simply just call `worker.fast_forward` to get us further in time: +`sandbox` offers support for forwarding the state of the blockchain to the future. This means contracts which require time sensitive data do not need to sit and wait the same amount of time for blocks on the sandbox to be produced. We can simply just call `sandbox.fast_forward` to get us further in time: + url="https://github.com/near-examples/near-workspaces-examples/blob/workspaces-migration/contract-rs/tests/basics.rs" + start="393" end="444" /> _[See the full example on Github](https://github.com/near/workspaces-rs/blob/main/examples/src/fast_forward.rs)._ @@ -423,7 +418,7 @@ This approach is more complex to do and also cannot be performed without restart ## Using Testnet -NEAR Workspaces is set up so that you can write tests once and run them against a local Sandbox node (the default behavior) or against [NEAR TestNet](../../protocol/network/networks.md). Some reasons this might be helpful: +NEAR Sandbox is set up so that you can write tests once and run them against a local Sandbox node (the default behavior) or against [NEAR TestNet](../../protocol/network/networks.md). Some reasons this might be helpful: * Gives higher confidence that your contracts work as expected * You can test against deployed testnet contracts @@ -433,8 +428,8 @@ NEAR Workspaces is set up so that you can write tests once and run them against + url="https://github.com/near-examples/near-workspaces-examples/blob/workspaces-migration/contract-rs/tests/basics.rs" + start="458" end="499" /> :::tip If you can create a new account on each iteration as well. @@ -530,7 +525,7 @@ NEAR Workspaces is set up so that you can write tests once and run them against ## Spooning Contracts -[Spooning a blockchain](https://coinmarketcap.com/alexandria/glossary/spoon-blockchain) is copying the data from one network into a different network. NEAR Workspaces makes it easy to copy data from Mainnet or Testnet contracts into your local Sandbox environment: +[Spooning a blockchain](https://coinmarketcap.com/alexandria/glossary/spoon-blockchain) is copying the data from one network into a different network. NEAR Sandbox makes it easy to copy data from Mainnet or Testnet contracts into your local Sandbox environment: @@ -541,8 +536,8 @@ NEAR Workspaces is set up so that you can write tests once and run them against Create a function called `pull_contract` which will pull the contract's `.wasm` file from the chain and deploy it onto your local sandbox. You'll have to re-initialize it with all the data to run tests. This is because the contract's data is too big for the RPC service to pull down. (limits are set to 50Mb) + url="https://github.com/near-examples/near-workspaces-examples/blob/workspaces-migration/contract-rs/tests/basics.rs" + start="507" end="537" /> @@ -581,7 +576,7 @@ Lets take a look at the test of our [Quickstart Project](../quickstart.md) [👋 + url="https://github.com/near-examples/hello-near-examples/blob/main/contract-rs/tests/test_basics.rs" start="1" end="71"/>