-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Matcher when using annotation #12
Comments
I just thought of one way we can solve for most use cases (mocking for any input). Add a Is this similar to your second possible solution ( Possibly unrelated, but there may be a precedence issue where the |
Yes this is the same thing as the second possible solution. This is probably the simplest solution to implement and the most flexible. As of the issue about some rules overriding others, I think other mocking libraries also have it. I would expect that the rules will be tested in the same order as they were added. |
In the following example, it seems to me that you would want the rules put in last to be used instead of the more general rule put in at the start: // Default case
mock.function
.given(Any)
.will_return(true);
// A more specific rule
mock.function
.given("test input")
.will_return(false);
assert_eq!(mock.function("test input"), false);
assert_eq!(mock.function("test input2"), true); Here is how Mockito, a popular Java mocking library, handles this:
|
I tried using two functions one #[mock_it]
trait MyTrait {
fn myFn(&self, aValue: i32, anothervalue: i32) -> i32;
} Would be: mock.given(Val((4, 4)).will_return(8); Instead of: mock.given((Val(4), Val(4)).will_return(8); And it would be impossible to do: mock.given((Val(4), Any).will_return(42); We need to find another way to support multiple inputs. |
I wonder if we could generate an implementation to tuples containing // Newtype for implementing things on tuples
struct MatcherTuple<T>(T);
// Repeat for tuples size 1-12
impl<A: PartialEq, B: PartialEq> PartialEq for MatcherTuple<(Matcher<A>, Matcher<B>)> {
fn eq(&self, other: &Matcher<I>) -> bool {
self.0.eq(&other.0) && self.1.eq(&other.1)
}
} Then we would change the mock rules to |
I think the easiest way would be to put everything in |
Can you give an example of how that would work? Is there a |
No I'm not proposing to make one. I think we should support |
It already generates that kind of code. Did you mean to link this instead? https://github.com/nathanielsimard/mock-it/blob/master/examples/any.rs |
Yes sorry about that. The generated code could also wraps the mock without the |
Do you mean to make a second mock object for each method which doesn't use matchers? Otherwise, the macro can't affect the ergonomics of the |
No just a wrapper object or function that wraps arguments into |
Arguments from when the mock is called, or when we configure the mock in the test? If the latter, it is impossible because we cannot abstract over generic tuples. |
From what I understand, you want to make a function that goes from |
When using
mock-it
annotation :There is no way to use matcher like :
Potential Solutions :
The text was updated successfully, but these errors were encountered: