pub trait CallReal {
fn call_real(&self) -> RealCallGuard<'_, Self> { ... }
fn call_real_once(&self) -> RealCallGuard<'_, Self> { ... }
}
Expand description
Controls delegation to real impls. The provided call_*
methods in this trait can be used
for partial mocking and spying.
This trait can be derived using the corresponding macro; it’s not intended
for manual implementation. The trait is also implemented for the Mut
and MockRef
wrappers.
Call guard checks
RealCallGuard
s returned by Self::call_real()
and Self::call_real_once()
must not overlap in terms of their lifetime; otherwise, confusion would arise as to
which calls exactly should be delegated to real implementations. This is checked
in runtime when creating a guard.
#[mock(using = "MyMock")]
fn answer() -> u32 { 42 }
#[derive(Default, Mock, CallReal)]
struct MyMock {
// mock state...
_switch: RealCallSwitch,
}
impl MyMock {
fn answer(&self) -> u32 {
let _guard = self.call_real();
let real_answer = self.call_real_once().scope(answer);
// ^ will panic here: there is an alive call switch guard
real_answer + 1
}
}
let _guard = MyMock::default().set_as_mock();
answer(); // triggers the panic
Provided Methods
sourcefn call_real(&self) -> RealCallGuard<'_, Self>
fn call_real(&self) -> RealCallGuard<'_, Self>
Delegates all calls to the mocked functions / methods to the real implementation until
the returned RealCallGuard
is dropped.
Panics
Panics if the real / mock implementation switch is already set to “real”
(e.g., there is an alive guard produced by an earlier call to Self::call_real()
).
This may lead to unexpected switch value for the further calls and is thus prohibited.
sourcefn call_real_once(&self) -> RealCallGuard<'_, Self>
fn call_real_once(&self) -> RealCallGuard<'_, Self>
Delegates the first call to the mocked functions / methods to the real implementation until
the returned RealCallGuard
is dropped. Further calls will be directed to the mock.
Panics
Panics under the same circumstances as Self::call_real()
.