Skip to content

Commit

Permalink
Fix e2e tests in the contract template (#1537)
Browse files Browse the repository at this point in the history
  • Loading branch information
smiasojed authored Mar 13, 2024
1 parent f87a257 commit 6f4c6dd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,6 @@ jobs:
# Build with linting
cargo run --profile debug-ci -- contract build --lint --manifest-path=${{ runner.temp }}/foobar/Cargo.toml &&
cargo run --profile debug-ci -- contract check --manifest-path=${{ runner.temp }}/foobar/Cargo.toml &&
cargo run --profile debug-ci -- contract build --manifest-path=${{ runner.temp }}/foobar/Cargo.toml --release
cargo run --profile debug-ci -- contract build --manifest-path=${{ runner.temp }}/foobar/Cargo.toml --release &&
# Run tests
cargo test --profile debug-ci --all-features -- --test-threads=1
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
Fix e2e tests in the contract template - [#1537](https://github.com/paritytech/cargo-contract/pull/1537)

## [4.0.0]

This `cargo-contract` release is compatible with Rust versions `>=1.70`and ink! versions `>=5.0.0`
Expand Down
6 changes: 2 additions & 4 deletions crates/build/templates/new/_Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ edition = "2021"
[dependencies]
ink = { version = "5.0.0", default-features = false }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true }
[dev-dependencies]
ink_e2e = { version = "5.0.0" }

[lib]
path = "lib.rs"
Expand All @@ -17,8 +17,6 @@ path = "lib.rs"
default = ["std"]
std = [
"ink/std",
"scale/std",
"scale-info/std",
]
ink-as-dependency = []
e2e-tests = []
45 changes: 22 additions & 23 deletions crates/build/templates/new/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ mod {{name}} {
use super::*;

/// A helper function used for calling contract messages.
use ink_e2e::build_message;
use ink_e2e::ContractsBackend;

/// The End-to-End test `Result` type.
type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
Expand All @@ -88,19 +88,19 @@ mod {{name}} {
#[ink_e2e::test]
async fn default_works(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
// Given
let constructor = {{camel_name}}Ref::default();
let mut constructor = {{camel_name}}Ref::default();

// When
let contract_account_id = client
.instantiate("{{name}}", &ink_e2e::alice(), constructor, 0, None)
let contract = client
.instantiate("{{name}}", &ink_e2e::alice(), &mut constructor)
.submit()
.await
.expect("instantiate failed")
.account_id;
.expect("instantiate failed");
let call_builder = contract.call_builder::<{{camel_name}}>();

// Then
let get = build_message::<{{camel_name}}Ref>(contract_account_id.clone())
.call(|{{name}}| {{name}}.get());
let get_result = client.call_dry_run(&ink_e2e::alice(), &get, 0, None).await;
let get = call_builder.get();
let get_result = client.call(&ink_e2e::alice(), &get).dry_run().await?;
assert!(matches!(get_result.return_value(), false));

Ok(())
Expand All @@ -110,30 +110,29 @@ mod {{name}} {
#[ink_e2e::test]
async fn it_works(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
// Given
let constructor = {{camel_name}}Ref::new(false);
let contract_account_id = client
.instantiate("{{name}}", &ink_e2e::bob(), constructor, 0, None)
let mut constructor = {{camel_name}}Ref::new(false);
let contract = client
.instantiate("{{name}}", &ink_e2e::bob(), &mut constructor)
.submit()
.await
.expect("instantiate failed")
.account_id;
.expect("instantiate failed");
let mut call_builder = contract.call_builder::<{{camel_name}}>();

let get = build_message::<{{camel_name}}Ref>(contract_account_id.clone())
.call(|{{name}}| {{name}}.get());
let get_result = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await;
let get = call_builder.get();
let get_result = client.call(&ink_e2e::bob(), &get).dry_run().await?;
assert!(matches!(get_result.return_value(), false));

// When
let flip = build_message::<{{camel_name}}Ref>(contract_account_id.clone())
.call(|{{name}}| {{name}}.flip());
let flip = call_builder.flip();
let _flip_result = client
.call(&ink_e2e::bob(), flip, 0, None)
.call(&ink_e2e::bob(), &flip)
.submit()
.await
.expect("flip failed");

// Then
let get = build_message::<{{camel_name}}Ref>(contract_account_id.clone())
.call(|{{name}}| {{name}}.get());
let get_result = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await;
let get = call_builder.get();
let get_result = client.call(&ink_e2e::bob(), &get).dry_run().await?;
assert!(matches!(get_result.return_value(), true));

Ok(())
Expand Down

0 comments on commit 6f4c6dd

Please sign in to comment.