Struct pseudo::Mock [−][src]
Used for tracking function call arguments and specifying a predetermined return value or mock function.
See the crate documentation for more substantial examples, including some
that demonstrate how to use Mock
for methods that have multiple arguments
as well as methods with argument or return types that do not implement
Clone
.
Methods
impl<C, R> Mock<C, R> where
C: Clone,
R: Clone,
[src]
impl<C, R> Mock<C, R> where
C: Clone,
R: Clone,
pub fn new<T: Into<R>>(return_value: T) -> Self
[src]
pub fn new<T: Into<R>>(return_value: T) -> Self
Creates a new Mock
that will return return_value
.
pub fn call(&self, args: C) -> R
[src]
pub fn call(&self, args: C) -> R
Use the Mock
to return a value, keeping track of the arguments used.
Depending on what has most recently been called, this will return:
- the return value specified at construction time
- the return value specified via
Mock::return_value
or a derivative, such asMock::return_some
- the output of the function set via
Mock::use_fn
with the current arguments - the output of the closure set via
Mock::use_closure
with the current arguments
Examples
use pseudo::Mock; let mock = Mock::<&str, _>::new("return value"); assert_eq!(mock.call("something"), "return value"); mock.return_value("different value"); assert_eq!(mock.call("something"), "different value"); mock.use_fn(str::trim); assert_eq!(mock.call(" test "), "test"); mock.use_closure(Box::new(|x| x.trim_left())); assert_eq!(mock.call(" test "), "test "); mock.use_fn(str::trim); assert_eq!(mock.call(" test "), "test");
pub fn return_value<T: Into<R>>(&self, return_value: T)
[src]
pub fn return_value<T: Into<R>>(&self, return_value: T)
Override the initial return value.
Examples
use pseudo::Mock; let mock = Mock::<&str, &str>::new("original value"); mock.return_value("new value"); assert_eq!(mock.call("something"), "new value");
pub fn use_fn(&self, mock_fn: fn(_: C) -> R)
[src]
pub fn use_fn(&self, mock_fn: fn(_: C) -> R)
Specify a function to determine the Mock
's return value based on
the arguments provided to Mock::call
.
Arguments of Mock::call
are still tracked.
Examples
use pseudo::Mock; fn add_two(x: i64) -> i64 { x + 2 } let mock = Mock::<i64, i64>::new(10); mock.use_fn(add_two); assert_eq!(mock.call(1), 3); assert_eq!(mock.call(10), 12);
For functions with multiple arguments, use a tuple:
use pseudo::Mock; fn add((x, y, z): (i64, i64, i64)) -> i64 { x + y + z } let mock = Mock::<(i64, i64, i64), i64>::default(); mock.use_fn(add); assert_eq!(mock.call((1, 1, 1)), 3); assert_eq!(mock.call((1, 2, 3,)), 6);
pub fn use_closure(&self, mock_fn: Box<Fn(C) -> R + Send + Sync>)
[src]
pub fn use_closure(&self, mock_fn: Box<Fn(C) -> R + Send + Sync>)
Specify a closure to determine the Mock
's return value based on
the arguments provided to Mock::call
.
Arguments of Mock::call
are still tracked.
Examples
use pseudo::Mock; let mock = Mock::<i64, i64>::new(10); let add_two = |x| x + 2; mock.use_closure(Box::new(add_two)); assert_eq!(mock.call(1), 3); assert_eq!(mock.call(10), 12);
For functions with multiple arguments, use a tuple:
use pseudo::Mock; let mock = Mock::<(i64, i64, i64), i64>::default(); let add = |(x, y, z)| x + y + z; mock.use_closure(Box::new(add)); assert_eq!(mock.call((1, 1, 1)), 3); assert_eq!(mock.call((1, 2, 3,)), 6);
pub fn called(&self) -> bool
[src]
pub fn called(&self) -> bool
Returns true if Mock::call
has been called.
Examples
use pseudo::Mock; let mock = Mock::<i64, ()>::default(); assert!(!mock.called()); // mock.call(10); // assert!(mock.called());
pub fn num_calls(&self) -> usize
[src]
pub fn num_calls(&self) -> usize
Returns the number of times Mock::call
has been called.
Examples
use pseudo::Mock; let mock = Mock::<i64, i64>::new(0); assert_eq!(mock.num_calls(), 0); mock.call(5); assert_eq!(mock.num_calls(), 1); mock.call(10); assert_eq!(mock.num_calls(), 2);
pub fn calls(&self) -> Vec<C>
[src]
pub fn calls(&self) -> Vec<C>
Returns the arguments to Mock::call
in order from first to last.
Examples
use pseudo::Mock; let mock = Mock::<&str, &str>::new(""); mock.call("first"); mock.call("second"); mock.call("third"); assert_eq!(mock.calls().as_slice(), ["first", "second", "third"]);
pub fn reset_calls(&self)
[src]
pub fn reset_calls(&self)
Reset the call history for the Mock
.
Examples
use pseudo::Mock; let mock = Mock::<&str, &str>::default(); mock.call("first"); mock.call("second"); assert!(mock.called()); assert_eq!(mock.num_calls(), 2); assert!(mock.called_with("first")); assert!(mock.called_with("second")); mock.reset_calls(); assert!(!mock.called()); assert_eq!(mock.num_calls(), 0); assert!(!mock.called_with("first")); assert!(!mock.called_with("second"));
impl<C, R> Mock<C, R> where
C: Clone + PartialEq,
R: Clone,
[src]
impl<C, R> Mock<C, R> where
C: Clone + PartialEq,
R: Clone,
pub fn called_with<T: Into<C>>(&self, args: T) -> bool
[src]
pub fn called_with<T: Into<C>>(&self, args: T) -> bool
Returns true if the specified argument has been used for Mock::call
.
Examples
use pseudo::Mock; let mock = Mock::<&str, ()>::new(()); mock.call("foo"); mock.call("bar"); assert!(mock.called_with("foo")); assert!(mock.called_with("bar")); assert!(!mock.called_with("baz"));
impl<C, S> Mock<C, Option<S>> where
C: Clone,
S: Clone,
[src]
impl<C, S> Mock<C, Option<S>> where
C: Clone,
S: Clone,
pub fn return_some<T: Into<S>>(&self, return_value: T)
[src]
pub fn return_some<T: Into<S>>(&self, return_value: T)
Return Some(return_value)
from Mock::call
.
Examples
use pseudo::Mock; let mock = Mock::<(), Option<i64>>::new(None); mock.return_some(10); assert_eq!(mock.call(()), Some(10));
pub fn return_none(&self)
[src]
pub fn return_none(&self)
Return None
from Mock::call
.
Examples
use pseudo::Mock; let mock = Mock::<(), Option<i64>>::new(Some(42)); mock.return_none(); assert_eq!(mock.call(()), None);
impl<C, O, E> Mock<C, Result<O, E>> where
C: Clone,
O: Clone,
E: Clone,
[src]
impl<C, O, E> Mock<C, Result<O, E>> where
C: Clone,
O: Clone,
E: Clone,
pub fn return_ok<T: Into<O>>(&self, return_value: T)
[src]
pub fn return_ok<T: Into<O>>(&self, return_value: T)
Return Ok(return_value)
from Mock::call
.
Examples
use pseudo::Mock; let mock = Mock::<(), Result<&str, &str>>::new(Err("oh no")); mock.return_ok("success"); assert_eq!(mock.call(()), Ok("success"));
pub fn return_err<T: Into<E>>(&self, return_value: T)
[src]
pub fn return_err<T: Into<E>>(&self, return_value: T)
Return Err(return_value)
from Mock::call
.
Examples
use pseudo::Mock; let mock = Mock::<(), Result<&str, &str>>::new(Ok("success")); mock.return_err("oh no"); assert_eq!(mock.call(()), Err("oh no"));
Trait Implementations
impl<C: Clone, R: Clone> Clone for Mock<C, R> where
C: Clone,
R: Clone,
[src]
impl<C: Clone, R: Clone> Clone for Mock<C, R> where
C: Clone,
R: Clone,
fn clone(&self) -> Mock<C, R>
[src]
fn clone(&self) -> Mock<C, R>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl<C, R> Default for Mock<C, R> where
C: Clone,
R: Clone + Default,
[src]
impl<C, R> Default for Mock<C, R> where
C: Clone,
R: Clone + Default,
fn default() -> Self
[src]
fn default() -> Self
Use R::default()
as the initial return value.
Examples
use pseudo::Mock; let mock = Mock::<i64, i64>::default(); assert_eq!(mock.call(10), 0); let mock = Mock::<(), String>::default(); assert_eq!(&mock.call(()), ""); let mock = Mock::<(i64, &str), Option<bool>>::default(); assert_eq!(mock.call((10, "test")), None);
impl<C, R> Debug for Mock<C, R> where
C: Clone + Debug,
R: Clone + Debug,
[src]
impl<C, R> Debug for Mock<C, R> where
C: Clone + Debug,
R: Clone + Debug,