Skip to content

Commit

Permalink
Tutorials > Polkadot SDK > Parachains > Build Custom Pallet > Pallet …
Browse files Browse the repository at this point in the history
…Unit Testing (#233)

* fix: wip

* fix: adding snippets

* fix: typo

* Apply suggestions from code review

Co-authored-by: 0xLucca <95830307+0xLucca@users.noreply.github.com>

* fix: updating mock runtime\

* fix: snippets comments

* fix: typo

* fix: closing expandable elements

* fix: overflow

* fix: overflow

* Update tutorials/polkadot-sdk/parachains/build-custom-pallet/pallet-unit-testing.md

Co-authored-by: Erin Shaben <eshaben@icloud.com>

* fix: feedback

* fix: clean up the add tutorial branch

* Update tutorials/polkadot-sdk/parachains/build-custom-pallet/pallet-unit-testing.md

Co-authored-by: Erin Shaben <eshaben@icloud.com>

---------

Co-authored-by: 0xLucca <95830307+0xLucca@users.noreply.github.com>
Co-authored-by: Erin Shaben <eshaben@icloud.com>
  • Loading branch information
3 people authored Dec 12, 2024
1 parent 4463223 commit 47d2f0d
Show file tree
Hide file tree
Showing 14 changed files with 522 additions and 528 deletions.
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ IGNORED_FILES=(
".snippets/code/develop/toolkit/parachains/spawn-chains/zombienet/write-tests/big-network-test.toml"
".snippets/code/develop/toolkit/parachains/spawn-chains/zombienet/write-tests/small-network-test.toml"
".snippets/code/develop/toolkit/parachains/spawn-chains/zombienet/write-tests/spawn-a-basic-chain-test.toml"
".snippets/code/tutorials/polkadot-sdk/parachains/build-custom-pallet/pallet-unit-testing/cargo-dev-dependencies.toml"
)

# Normalize ignored files pattern
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[dependencies]
...

[dev-dependencies]
sp-core = { workspace = true, default-features = true }
sp-io = { workspace = true, default-features = true }
sp-runtime = { workspace = true, default-features = true }

[features]
...
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate as pallet_custom;
use frame_support::{derive_impl, parameter_types};
use sp_runtime::BuildStorage;

type Block = frame_system::mocking::MockBlock<Test>;

#[frame_support::runtime]
mod runtime {
#[runtime::runtime]
#[runtime::derive(
RuntimeCall,
RuntimeEvent,
RuntimeError,
RuntimeOrigin,
RuntimeFreezeReason,
RuntimeHoldReason,
RuntimeSlashReason,
RuntimeLockId,
RuntimeTask
)]
pub struct Test;

#[runtime::pallet_index(0)]
pub type System = frame_system::Pallet<Test>;

#[runtime::pallet_index(1)]
pub type CustomPallet = pallet_custom::Pallet<Test>;
}

// System pallet configuration
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Test {
type Block = Block;
}

// Custom pallet configuration
parameter_types! {
pub const CounterMaxValue: u32 = 10;
}

impl pallet_custom::Config for Test {
type RuntimeEvent = RuntimeEvent;
type CounterMaxValue = CounterMaxValue;
}

// Test externalities initialization
pub fn new_test_ext() -> sp_io::TestExternalities {
frame_system::GenesisConfig::<Test>::default()
.build_storage()
.unwrap()
.into()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div id="termynal" data-termynal>
<span data-ty="input"><span class="file-path"></span>cargo test --package custom-pallet</span>
<pre>
running 12 tests
test mock::__construct_runtime_integrity_test::runtime_integrity_tests ... ok
test mock::test_genesis_config_builds ... ok
test test::set_counter_value_fails_for_max_value_exceeded ... ok
test test::set_counter_value_fails_for_non_root ... ok
test test::user_interactions_increment ... ok
test test::it_works_for_increment ... ok
test test::it_works_for_set_counter_value ... ok
test test::it_works_for_decrement ... ok
test test::increment_handles_overflow ... ok
test test::decrement_fails_for_below_zero ... ok
test test::increment_fails_for_max_value_exceeded ... ok
test test::user_interactions_overflow ... ok
test result: ok. 12 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s

Doc-tests custom_pallet
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
</pre
>
</div>
Loading

0 comments on commit 47d2f0d

Please sign in to comment.