[−][src]Struct mocktopus::mocking::MockContext
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]
F: Mockable<I, O>,
M: FnMut<I, Output = MockResult<I, O>> + 'a,
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]
F: Mockable<I, O>,
M: FnMut<I, Output = MockResult<I, O>>,
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]
pub fn default() -> 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]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,