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

Add support for detecting unused setups #747

Closed
dfev77 opened this issue Feb 8, 2019 · 3 comments
Closed

Add support for detecting unused setups #747

dfev77 opened this issue Feb 8, 2019 · 3 comments

Comments

@dfev77
Copy link

dfev77 commented Feb 8, 2019

After a while there is a big chance (especially when dealing with legacy code) to have tons of mock setups that are actually not used.
It would be nice to be able to spot such case in order to cleanup the code.

An option would be to allow the test(s) to fail if there were any mocks which had setups that were not called (at the point where the mock gets out of scope)

@stakx
Copy link
Contributor

stakx commented Feb 8, 2019

An option would be to allow the test(s) to fail if there were any mocks which had setups that were not called

If you get your mocks from a MockRepository instead of new-ing them up, that should be trivial:

mockRepository.VerifyAll();

at the point where the mock gets out of scope

.NET does not have deterministic resource cleanup like that (we'd need something like C++'s RAII idiom) so that is impossible to do. The closest thing in .NET is the IDisposable protocol, but it's far too late in the game to make Mock<T> implement IDisposable.

In all honesty, if your tests get so large that you no longer know what mocks and setups you have, then that is the problem to solve; putting a bandaid on it (in the form of a library feature) is not a good way to deal with this (IMO). Refactor your test code, instead.

That being said, this problem might be tackled with a Roslyn analyzer.

@stakx
Copy link
Contributor

stakx commented Feb 16, 2019

@dfev77, any thoughts about the above reply? Otherwise I'll close this issue in a couple of days, since there's not much that can be done (short of rigging setups with a TimebombException that goes off when the setup is not invoked for a set amount of time—and I don't think that would make for a very good feature :-D).

@stakx stakx closed this as completed Feb 20, 2019
@stakx
Copy link
Contributor

stakx commented Feb 20, 2019

Closing due to a lack of feedback (and lack of implementation options). If you'd like to further discuss this or make additional suggestions, feel free to post here -- we can always reopen the issue.

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