Skip to content
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

Mock.Object is slow for some types #830

Closed
acesiddhu opened this issue May 21, 2019 · 3 comments
Closed

Mock.Object is slow for some types #830

acesiddhu opened this issue May 21, 2019 · 3 comments

Comments

@acesiddhu
Copy link

Hi Team

we are using Mock in our code base and we have noticed that Mock.Object call is slow for certain types. I have created a repro the issue here https://github.com/acesiddhu/MockHttpClients
Do we know why the call is slow? any thing we can do on our side to fix this issue?

@stakx
Copy link
Contributor

stakx commented May 22, 2019

@acesiddhu: The speed of accessing mock.Object for the first time for a given mock instance is mostly determined by the work done by the proxy library used (Castle DynamicProxy). Creating a proxy type will take longer the more members the mocked type has.

Will look at your repo at a later time but chances are there's generally not much you can do about perf.

@acesiddhu
Copy link
Author

@stakx sure let me know. if there are any workaround we can try please suggest those as well

@stakx
Copy link
Contributor

stakx commented May 24, 2019

OK, judging from your repo, it seems that you have already figured out that new Mock<T>(...) is faster than Mock.Of.

You might already have noticed that SetupAllProperties isn't very fast (and it's called by Mock.Of behind the scenes).

If you want to increase mocking speed, you could define role interfaces for the capabilities used in e.g. GitHttpClient, and use dependency injection in your code base to inject the concrete type for the role interface. This would allow you (in your test code) to mock only the role interface instead of the actual concrete class. This should be faster because the role interface will in all likelihood contain only very few members (see my first comment above).

I don't have any further suggestions apart from that.

@stakx stakx closed this as completed May 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants