1

I am using the balances pallet while testing my runtime and custom pallet(s).

...
/pallets
  /things
    /src
      lib.rs
      mock.rs
      tests.rs
/runtime
  /src
    lib.rs
    mock.rs
    tests.rs
...
// runtime/src/tests.rs
use crate::{
    mock::*,
    sp_api_hidden_includes_construct_runtime::hidden_include::traits::Currency,
};
use frame_support::{assert_noop, assert_ok};

#[test]
fn balance_pallet_works() {
    new_test_ext().execute_with(|| {
        let _ = Balances::deposit_creating(&1, 10);
        assert_eq!(Balances::free_balance(1), 10);
    });
}

...

This works. I was warned to add use crate::sp_api_hidden_includes_construct_runtime::hidden_include::traits::Currency to implement (within scope) the trait requiring deposit_creating.

However, the same pattern in pallets/jobs/src/tests.rs is recommending that I import from its runtime implementation mock.

// pallets/things/src/tests.rs
use crate::{
    mock::*,
};
use frame_support::{assert_err, assert_ok};
// use crate::*mock::*sp_api_hidden_includes_construct_runtime::hidden_include::traits::Currency;

#[test]
fn things_pallet_works() {
    new_test_ext().execute_with(|| {
        let _ = Balances::deposit_creating(&1, 100);
        assert_eq!(Things::<Test>::count(), 0);
    });
}

let _ = Balances::deposit_creating(&1, 100); reports the error use crate::mock::sp_api_hidden_includes_construct_runtime::hidden_include::traits::Currency; which, when I use as suggested, errors with error[E0603]: module sp_api_hidden_includes_construct_runtime is private.

I'm confused as to why the import recommendation is for the crate in my runtime tests yet crate::mocks for my pallet tests clearly due to a gap in my understanding of Rust's module system.

3 Answers 3

3

First of all, deposit_creating is not a function exposed by Balance, but a function from the Currency trait.

So, to use it, first thing to do is to use the Balance pallet as the Currency trait in your config (if you have the type on your pallet's config), so in your mock, you should have:

frame_support::construct_runtime!(
    pub enum Test where
        Block = Block,
        NodeBlock = Block,
        UncheckedExtrinsic = UncheckedExtrinsic,
    {
        System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
        Whatever: pallet_whatever::{Pallet, Call, Storage, Event<T>},

        Balances: pallet_balances, // you might need to add pallet_balances to your cargo.toml
    }
);

impl pallet_whatever::Config for Test {
    ...
    type Currency = Balances;
    ...
}

then, in your test, import the Currency trait (you cannot use trait's function if the trait is not in scope)

use frame_support::traits::Currency;

then, call the function on the trait

use frame_support::traits::Currency;
use crate::{Config as MyConfig}; // I use a named import for convenience

...

let imbalance = <Test as MyConfig>::Currency::deposit_creating(&ALICE(), amount.into());
1

You just need to import

use frame_support::traits::Currency;

As it has the functionality that it requires.

0

maybe you can try mint_into instead which is a trait method from frame_support::traits::tokens::fungible::Mutate<AccountId> if recall correct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.