[−][src]Crate mockiato
A strict, yet friendly mocking library for Rust 2018
Examples
use mockiato::mockable; #[cfg_attr(test, mockable)] trait Greeter { fn greet(&self, name: &str) -> String; } let mut greeter = GreeterMock::new(); greeter .expect_greet(|arg| arg.partial_eq("Jane")) .times(1..) .returns(String::from("Hello Jane")); assert_eq!("Hello Jane", greeter.greet("Jane"));
Configuring Expected Calls
Each method on the trait receives two companion methods on the mock struct:
expect_<method_name>
Registers an expected call to the mocked method. Exactly one call is expected by default. The order in which these methods are called is irrelevant, unless configured otherwise.
It has the same amount of arguments as the mocked method.
Each argument accepts a closure that is invoked with a reference to Argument
, which lets
you create different argument matchers.
This method returns a MethodCallBuilder
which allows for further customization of an expected call's behavior.
use mockiato::mockable; #[mockable] trait MessageSender { fn send_message(&self, recipient: &str, message: &str); } let mut message_sender = MessageSenderMock::new(); message_sender .expect_send_message(|arg| arg.partial_eq("Paul"), |arg| arg.any()) .times(..);
expect_<method_name>_calls_in_order
Configures the mocked method so that the expected calls are processed sequentially.
When this is enabled, the calls to the mocked method must be in the same order as the expect_
methods were called.
message_sender.expect_send_message_calls_in_order();
Call Verification
Mockiato automatically verifies that all expected calls were made when the mock goes out of scope. The mock panics when a method is called that was not configured, or if the parameters did not match.
use mockiato::mockable; #[cfg_attr(test, mockable)] trait Greeter { fn greet(&self, name: &str) -> String; } { let mut greeter = GreeterMock::new(); greeter .expect_greet(|arg| arg.partial_eq("Doe")) .times(1..) .returns(String::from("Hello Doe")); assert_eq!("Hello Jane", greeter.greet("Jane")); // ^^^^^^^^^^^^^ // This call was not configured, which results in a panic // The mock verifies that all expected calls have been made // <-- and panics otherwise }
Macros
mockable | Generates a mock struct from a trait. |
Structs
Argument | A factory for creating argument matchers |
ExpectedCalls | Defines how often a method call is expected
to be called.
See |
MethodCallBuilder | Configures an expected method call.
This builder is returned from the |