1234567891011use frame_support::{decl_module, decl_storage, decl_event, dispatch::DispatchResult};12use system::ensure_signed;131415pub trait Trait: system::Trait {16 1718 19 type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;20}212223decl_storage! {24 trait Store for Module<T: Trait> as TemplateModule {25 26 27 28 Something get(fn something): Option<u32>;29 }30}313233decl_module! {34 35 pub struct Module<T: Trait> for enum Call where origin: T::Origin {36 37 38 fn deposit_event() = default;3940 41 42 43 pub fn do_something(origin, something: u32) -> DispatchResult {44 45 let who = ensure_signed(origin)?;4647 48 49 Something::put(something);5051 52 Self::deposit_event(RawEvent::SomethingStored(something, who));53 Ok(())54 }55 }56}5758decl_event!(59 pub enum Event<T> where AccountId = <T as system::Trait>::AccountId {60 61 62 63 SomethingStored(u32, AccountId),64 }65);666768#[cfg(test)]69mod tests {70 use super::*;7172 use sp_core::H256;73 use frame_support::{impl_outer_origin, assert_ok, parameter_types, weights::Weight};74 use sp_runtime::{75 traits::{BlakeTwo256, IdentityLookup}, testing::Header, Perbill,76 };7778 impl_outer_origin! {79 pub enum Origin for Test {}80 }8182 83 84 85 #[derive(Clone, Eq, PartialEq)]86 pub struct Test;87 parameter_types! {88 pub const BlockHashCount: u64 = 250;89 pub const MaximumBlockWeight: Weight = 1024;90 pub const MaximumBlockLength: u32 = 2 * 1024;91 pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);92 }93 impl system::Trait for Test {94 type Origin = Origin;95 type Call = ();96 type Index = u64;97 type BlockNumber = u64;98 type Hash = H256;99 type Hashing = BlakeTwo256;100 type AccountId = u64;101 type Lookup = IdentityLookup<Self::AccountId>;102 type Header = Header;103 type Event = ();104 type BlockHashCount = BlockHashCount;105 type MaximumBlockWeight = MaximumBlockWeight;106 type MaximumBlockLength = MaximumBlockLength;107 type AvailableBlockRatio = AvailableBlockRatio;108 type Version = ();109 type ModuleToIndex = ();110 }111 impl Trait for Test {112 type Event = ();113 }114 type TemplateModule = Module<Test>;115116 117 118 fn new_test_ext() -> sp_io::TestExternalities {119 system::GenesisConfig::default().build_storage::<Test>().unwrap().into()120 }121122 #[test]123 fn it_works_for_default_value() {124 new_test_ext().execute_with(|| {125 126 127 assert_ok!(TemplateModule::do_something(Origin::signed(1), 42));128 129 assert_eq!(TemplateModule::something(), Some(42));130 });131 }132}