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

Fix e2e tests in the contract template #1537

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
Loading