pub struct MockGuard<T: Mock> { /* private fields */ }
Expand description
Exclusive guard to set the mock state.
A guard can be used to check / adjust the mock state during the test. Dropping the guard also unsets the mock state, so that targeted functions are no longer mocked.
In case of shared mocks, guards also provided synchronization across concurrently
executing tests: until a guard is dropped, other threads attempting
to call Mock::set_as_mock()
will block. Unfortunately, this is not always sufficient
to have good results; see Shared
docs for discussion.
Examples
#[mock(using = "ValueMock")]
fn answer() -> usize { 42 }
#[derive(Default, Mock)]
struct ValueMock(usize);
impl CheckRealCall for ValueMock {}
impl ValueMock {
fn answer(&self) -> usize {
self.0
}
}
assert_eq!(answer(), 42);
let mut guard: MockGuard<_> = ValueMock::default().set_as_mock();
assert_eq!(answer(), 0);
guard.with(|mock| { mock.0 = 23; });
// ^ updates mock state without releasing the guard
assert_eq!(answer(), 23);
Implementations
sourceimpl<T: Mock> MockGuard<T>
impl<T: Mock> MockGuard<T>
sourcepub fn with<R>(&mut self, action: impl FnOnce(&mut T) -> R) -> R
pub fn with<R>(&mut self, action: impl FnOnce(&mut T) -> R) -> R
Performs an action on the mock state without releasing the guard. This can be used to adjust the mock state, check or take some parts of it (such as collected args or responses).
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Returns the enclosed mock state and releases the exclusive lock.
Trait Implementations
Auto Trait Implementations
impl<T> RefUnwindSafe for MockGuard<T>where
<<T as Mock>::Shared as SetMock<'static, <T as Mock>::Base>>::Guard: RefUnwindSafe,
impl<T> Send for MockGuard<T>where
<<T as Mock>::Shared as SetMock<'static, <T as Mock>::Base>>::Guard: Send,
impl<T> Sync for MockGuard<T>where
<<T as Mock>::Shared as SetMock<'static, <T as Mock>::Base>>::Guard: Sync,
impl<T> Unpin for MockGuard<T>where
<<T as Mock>::Shared as SetMock<'static, <T as Mock>::Base>>::Guard: Unpin,
impl<T> UnwindSafe for MockGuard<T>where
<<T as Mock>::Shared as SetMock<'static, <T as Mock>::Base>>::Guard: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more