[][src]Struct mocktopus::mocking::MockContext

pub struct MockContext<'a> { /* fields omitted */ }

MockContext allows for safe capture of local variables.

It does this by forcing only mocking the actual function while in the body of run.

Examples

Simple function replacement:

use mocktopus::macros::mockable;
use mocktopus::mocking::{MockContext, MockResult};

#[mockable]
fn f() -> i32 {
    0
}

MockContext::new()
    .mock_safe(f, || MockResult::Return(1))
    .run(|| {
        assert_eq!(f(), 1);
    });

Using local variables:

use mocktopus::macros::mockable;
use mocktopus::mocking::{MockContext, MockResult};

#[mockable]
fn as_str(s: &String) -> &str {
    &s
}

let mut count = 0;
MockContext::new()
    .mock_safe(as_str, |s| { count += 1; MockResult::Return(&s) })
    .run(|| {
        assert_eq!(as_str(&"abc".to_string()), "abc");
    });
assert_eq!(count, 1);

Implementations

impl<'a> MockContext<'a>[src]

pub fn new() -> Self[src]

Create a new MockContext object.

pub fn mock_safe<I, O, F, M>(self, mockable: F, mock: M) -> Self where
    F: Mockable<I, O>,
    M: FnMut<I, Output = MockResult<I, O>> + 'a, 
[src]

Set up a function to be mocked.

This function doesn't actually mock the function. It registers it as a function that will be mocked when run is called.

pub unsafe fn mock_raw<I, O, F, M>(self, mockable: F, mock: M) -> Self where
    F: Mockable<I, O>,
    M: FnMut<I, Output = MockResult<I, O>>, 
[src]

Set up a function to be mocked.

This is an unsafe version of mock_safe, without lifetime constraint on mock

pub fn run<T, F: FnOnce() -> T>(self, f: F) -> T[src]

Run the function while mocking all the functions.

This function will mock all functions registered for mocking, run the function passed in, then deregister those functions. It does this in a panic-safe way. Note that functions are only mocked in the current thread and other threads may invoke the real implementations.

Register a function for mocking with mock_safe.

Trait Implementations

impl<'a> Default for MockContext<'a>[src]

Auto Trait Implementations

impl<'a> !RefUnwindSafe for MockContext<'a>[src]

impl<'a> !Send for MockContext<'a>[src]

impl<'a> !Sync for MockContext<'a>[src]

impl<'a> Unpin for MockContext<'a>[src]

impl<'a> !UnwindSafe for MockContext<'a>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.