Skip to content

Commit

Permalink
Update readme and add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielsimard committed Apr 9, 2018
1 parent b824e37 commit ffeee26
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ description = "Make mocking reliable"
repository = "https://github.com/nathanielsimard/mock-it"
license = "MIT"
readme="README.md"
keywords = ["mock", "mocking", "unit-test", "testing"]
categories = ["development-tools::testing"]

[dependencies]

[dev-dependencies]
table-test = "0.2.0"
table-test = "0.2.1"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ The first call will return the default value which is an error and the second ca
If we want to do assertion, it is also possible.

```rust
assert!(
assert!(verify(
person_factory_mock
.create
.was_called_with((String::from("MyName"), String::from("MySurname")))
);
));
```

That's it ! If you want more examples, just check [here](examples).
24 changes: 24 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#[macro_export]
macro_rules! verify {
( $( $x:expr ),* ) => {
{
let mut result = true
$(
temp_vec.push($x);
)*
temp_vec
}
};
}



#[cfg(test)]
mod test{
use super::*;

#[test]
fn it_works(){
verify!(validator)
}
}
35 changes: 32 additions & 3 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ extern crate mock_it;
#[macro_use]
extern crate table_test;

use mock_it::Matcher;
use mock_it::Matcher::*;
use mock_it::*;

#[derive(PartialEq, Debug, Clone)]
Expand All @@ -25,7 +27,7 @@ trait PersonFactory {

#[derive(Clone)]
struct PersonFactoryMock {
create: Mock<(String, String), Result<Person, String>>,
create: Mock<(Matcher<String>, Matcher<String>), Result<Person, String>>,
}

impl PersonFactoryMock {
Expand All @@ -38,7 +40,8 @@ impl PersonFactoryMock {

impl PersonFactory for PersonFactoryMock {
fn create(&self, name: String, surname: String) -> Result<Person, String> {
self.create.called((name.clone(), surname.clone()))
self.create
.called((Val(name.clone()), Val(surname.clone())))
}
}

Expand All @@ -61,7 +64,7 @@ fn will_return() {

person_factory_mock
.create
.given((name.clone(), surname.clone()))
.given((Val(name.clone()), Val(surname.clone())))
.will_return(Ok(Person::new(name.clone(), surname.clone())));

let actual = person_factory.create(name.clone(), surname.clone());
Expand All @@ -72,3 +75,29 @@ fn will_return() {
.assert_eq(person, actual);
}
}

#[test]
fn given_person_factory_mock_then_call_it_4_times_with_any_values_then_validate_times_4_return_true(
) {
let times = 4;
let name = "John".to_string();
let surname = "Bouchard".to_string();
let person_factory_mock = PersonFactoryMock::new();
let person_factory = Box::new(person_factory_mock.clone());

person_factory_mock
.create
.given((Val(name.clone()), Val(surname.clone())))
.will_return(Ok(Person::new(name.clone(), surname.clone())));

for i in 0..times {
let _ = person_factory.create(name.clone(), i.to_string());
}

assert!(verify(
person_factory_mock
.create
.was_called_with((Any, Any))
.times(times),
));
}

0 comments on commit ffeee26

Please sign in to comment.