pub struct MockFn<State> { /* private fields */ }
Expand description

Holds a function used for mocking invocations of contracts with invoke_contract.

Implementations§

source§

impl<State> MockFn<State>

source

pub fn new<R, F>(mock_fn_return: F) -> Selfwhere
R: Serial,
F: Fn(Parameter<'_>, Amount, &mut Amount, &mut State) -> CallContractResult<R> + 'static,

Create a mock function which has access to parameter, amount, balance, and state.

parameter and amount correspond to the values used in invoke_contract(.., parameter, .., amount). balance and state correspond to the values from the contract you are testing. They are used to simulate calls to the contract itself, which can change the balance and state of the contract.

The function should return a pair (state_modified, return_value), where state_modified should be set to true, if the function modifies the state parameter. It should modify the balance and state in way desired in the test, or in a way that the called contract is intended to behave.

See also returning_ok and returning_err for when you need simple mocks.

source

pub fn new_v1<R, F>(mock_fn_return: F) -> Selfwhere
R: Serial,
F: Fn(Parameter<'_>, Amount, &mut Amount, &mut State) -> Result<(bool, R), CallContractError<R>> + 'static,

A helper that assumes that a V1 contract is invoked. This means that the return value will always be present in case of success.

source

pub fn new_v0<R, F>(mock_fn_return: F) -> Selfwhere
R: Serial,
F: Fn(Parameter<'_>, Amount, &mut Amount, &mut State) -> Result<bool, CallContractError<R>> + 'static,

A helper that assumes that a V0 contract is invoked. This means that the return value will never be present in case of success, and hence does not have to be provided by the caller.

source

pub fn returning_ok<R: Clone + Serial + 'static>(return_value: R) -> Self

Create a simple mock function that returns Ok with the same value every time, and signals the state is not changed.

source

pub fn returning_err<R: Clone + Serial + 'static>(
error: CallContractError<R>
) -> Self

Create a simple mock function that returns Err with same error every time.

Auto Trait Implementations§

§

impl<State> !RefUnwindSafe for MockFn<State>

§

impl<State> !Send for MockFn<State>

§

impl<State> !Sync for MockFn<State>

§

impl<State> Unpin for MockFn<State>

§

impl<State> !UnwindSafe for MockFn<State>

Blanket Implementations§

source§

impl<T> Any for Twhere
T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere
U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.