This repository was archived by the owner on Jun 30, 2023. It is now read-only.
Add analyzer and codefix to improve setup scopes #49
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We added the parameterless
mock.Setup()to make it more discoverable how to switch the entire mock(s) to setup mode so the invocation tracking and recording and strict mock validation doesn't kick in, while you still get to use the lamba-free way of setting things up.That pattern, however, could be confusing and lead you to believe you need one such Setup() call for every mock being set up in the using block. This is not the case: the setup scope is active for the entire call-context/thread, so there's no need for the method to be called on a particular instance of one mock.
This analyzer and codefix makes this more obvious and also introduces users to the Syntax static helper, which should make the test code also much more succint since it has optimized members for shorter syntax (i.e. for Verify, Raise, Any/Not and so on). Notice the added
using static Moq.Syntax;at the top of the file when the codefix is applied:The added helpers are mostly brought as-is from Stunts, but with a simplified folder structure for individual analyzer codes.