[][src]Macro sp_api::mock_impl_runtime_apis

mock_impl_runtime_apis!() { /* proc-macro */ }

Mocks given trait implementations as runtime apis.

Accepts similar syntax as impl_runtime_apis! and generates simplified mock implementations of the given runtime apis. The difference in syntax is that the trait does not need to be referenced by a qualified path, methods accept the &self parameter and the error type can be specified as associated type. If no error type is specified String is used as error type.

Besides implementing the given traits, the [Core], [ApiExt] and [ApiErrorExt] are implemented automatically.

Example

use sp_version::create_runtime_str;

struct MockApi {
    balance: u64,
}

/// All runtime api mock implementations need to be done in one call of the macro!
sp_api::mock_impl_runtime_apis! {
    impl Balance<Block> for MockApi {
        /// Here we take the `&self` to access the instance.
        fn get_balance(&self) -> u64 {
            self.balance
        }
        fn set_balance(_bal: u64) {
            // Store the balance
        }
    }

    impl BlockBuilder<Block> for MockApi {
        /// Sets the error type that is being used by the mock implementation.
        /// The error type is used by all runtime apis. It is only required to
        /// be specified in one trait implementation.
        type Error = String;

        fn build_block() -> Block {
             unimplemented!("Not Required in tests")
        }
    }
}