-
Notifications
You must be signed in to change notification settings - Fork 14
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
ProxyMock - mock that intercepts calls to existing object #24
Comments
Pull request for this feature is here #27 |
I've tried to implement something similar, you can look at it at jm-partial-mocks branch: https://github.com/janmarek/mockista/compare/jm-partial-mocks. Unfortunately I've realized that it needs to be completely rewritten to pass the last test. Mocking methods called from not mocked method is indeed a useful function and it can be achieved only by inheritance, not composition. So method delegation has to be done by generated code by ClassGenerator. |
But using inheritance you still have to construct whole new object. I think it is bit different use case than proxy mocks. Main motivation here was to only intercept calls to existing, already initalized, objects. We use this mainly in presenters testing where we create proxy mocks of presenter dependencies. Then we can assert only important calls to these dependencies if we want but do not have to create them on our own. Partial mocks are good idea too, but they are not so useful in case when mocked object have lot of dependencies. |
This should be merged. |
👍 @MartinMystikJonas Any progress? |
@MartinMystikJonas Do you think we can merge it? |
Not sure. We use it in our own fork but I did not update upstream for long time. |
This is feature we just working on. It is not final yet, but I would like to have your input.
Working name for this is ProxyMock.
Main idea is that sometimes you do not want your mock object to mock all calls for some object just intercepts some calls and let everything else pass to some real instance. Basically in test you only care about part of the object behaviour and do not want to be forced to set expects for all calls.
Most useful is this if mocked object has some complex behavior or computation or if call sequence and/or internal state is important for return values.
Creating of proxy mock is simple. Just pass existing instance instead of class/interface name when creating mock:
When we set no expects it will just passthrought all calls to original object:
We can set expect for some calls and return different values:
Of couse we can set expectations in number of calls as normal
For case when we want to set expectation but still return value computed by original object, new and* method will be added:
Of course we can use some fake implementation of original object as well:
So what do you think?
The text was updated successfully, but these errors were encountered: