pub struct Mock<C, R>{ /* private fields */ }
Expand description
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
.
Implementations§
Source§impl<C, R> Mock<C, R>
impl<C, R> Mock<C, R>
Sourcepub fn new<T: Into<R>>(return_value: T) -> Self
pub fn new<T: Into<R>>(return_value: T) -> Self
Creates a new Mock
that will return return_value
.
Sourcepub fn call(&self, args: C) -> R
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");
Examples found in repository?
More examples
Sourcepub fn return_value<T: Into<R>>(&self, return_value: T)
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");
Sourcepub fn use_fn(&self, mock_fn: fn(C) -> R)
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);
Sourcepub fn use_closure(&self, mock_fn: Box<dyn Fn(C) -> R + Send + Sync>)
pub fn use_closure(&self, mock_fn: Box<dyn 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);
Sourcepub fn called(&self) -> bool
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());
Sourcepub fn num_calls(&self) -> usize
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);
Examples found in repository?
48fn main() {
49 let mock = MockDependency::default();
50 let consumer = Consumer::new(&mock);
51
52 consumer.greet_everyone(vec!["Fido", "Spot", "Princess"]);
53
54 assert_eq!(mock.greet.num_calls(), 3);
55
56 assert!(mock.greet.called_with("Fido"));
57 assert!(mock.greet.called_with("Spot"));
58 assert!(mock.greet.called_with("Princess"));
59}
More examples
62fn main() {
63 // Initially, the `copy` mock returns `Ok()`
64 let mock = MockFileSystem::default();
65
66 let result = copy_to_all(&mock, "from", &["to"]);
67
68 assert!(result.iter().all(|res| res.is_ok()));
69
70 assert_eq!(mock.copy.num_calls(), 1);
71 let expected_args = (
72 Path::new("from").to_path_buf(),
73 Path::new("to").to_path_buf(),
74 );
75 assert!(mock.copy.called_with(expected_args));
76
77 let err = CloneableError {
78 kind: ErrorKind::NotFound,
79 description: "test",
80 };
81 mock.copy.return_err(err);
82
83 let result = copy_to_all(&mock, "from", &["to"]);
84
85 assert!(result.iter().all(|res| res.is_err()));
86}
Sourcepub fn calls(&self) -> Vec<C>
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"]);
Sourcepub fn reset_calls(&self)
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"));
Source§impl<C, R> Mock<C, R>
impl<C, R> Mock<C, R>
Sourcepub fn called_with<T: Into<C>>(&self, args: T) -> bool
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"));
Examples found in repository?
48fn main() {
49 let mock = MockDependency::default();
50 let consumer = Consumer::new(&mock);
51
52 consumer.greet_everyone(vec!["Fido", "Spot", "Princess"]);
53
54 assert_eq!(mock.greet.num_calls(), 3);
55
56 assert!(mock.greet.called_with("Fido"));
57 assert!(mock.greet.called_with("Spot"));
58 assert!(mock.greet.called_with("Princess"));
59}
More examples
62fn main() {
63 // Initially, the `copy` mock returns `Ok()`
64 let mock = MockFileSystem::default();
65
66 let result = copy_to_all(&mock, "from", &["to"]);
67
68 assert!(result.iter().all(|res| res.is_ok()));
69
70 assert_eq!(mock.copy.num_calls(), 1);
71 let expected_args = (
72 Path::new("from").to_path_buf(),
73 Path::new("to").to_path_buf(),
74 );
75 assert!(mock.copy.called_with(expected_args));
76
77 let err = CloneableError {
78 kind: ErrorKind::NotFound,
79 description: "test",
80 };
81 mock.copy.return_err(err);
82
83 let result = copy_to_all(&mock, "from", &["to"]);
84
85 assert!(result.iter().all(|res| res.is_err()));
86}
Source§impl<C, S> Mock<C, Option<S>>
impl<C, S> Mock<C, Option<S>>
Sourcepub fn return_some<T: Into<S>>(&self, return_value: T)
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));
Sourcepub fn return_none(&self)
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);
Source§impl<C, O, E> Mock<C, Result<O, E>>
impl<C, O, E> Mock<C, Result<O, E>>
Sourcepub fn return_ok<T: Into<O>>(&self, return_value: T)
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"));
Sourcepub fn return_err<T: Into<E>>(&self, return_value: T)
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"));
Examples found in repository?
62fn main() {
63 // Initially, the `copy` mock returns `Ok()`
64 let mock = MockFileSystem::default();
65
66 let result = copy_to_all(&mock, "from", &["to"]);
67
68 assert!(result.iter().all(|res| res.is_ok()));
69
70 assert_eq!(mock.copy.num_calls(), 1);
71 let expected_args = (
72 Path::new("from").to_path_buf(),
73 Path::new("to").to_path_buf(),
74 );
75 assert!(mock.copy.called_with(expected_args));
76
77 let err = CloneableError {
78 kind: ErrorKind::NotFound,
79 description: "test",
80 };
81 mock.copy.return_err(err);
82
83 let result = copy_to_all(&mock, "from", &["to"]);
84
85 assert!(result.iter().all(|res| res.is_err()));
86}
Trait Implementations§
Source§impl<C, R> Default for Mock<C, R>
impl<C, R> Default for Mock<C, R>
Source§fn default() -> Self
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);