Fixed GetEnumerator() method on mock #3999
Merged
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.
Currently the underlying data collection's
GetEnumerator()
is only invoked once, and the resulting value is used every time the mockedDbSet
'sGetEnumerator()
method is invoked. The mockedDbSet
needs to be configured to retrieve a new enumerator each time the method is called by invoking the underlying data collection'sGetEnumerator()
method. Otherwise the enumerator returned by the method will become invalid if the contents of the mockedDbSet
change.Since the example sets up the underlying data before configuring the mock, and doesn't make any subsiquent changes after that point, this doesn't cause any issues in the example. But if the same setup code is used in a unit test that involves modifying the contents of the DbSet, the code will throw
InvalidOperationException
s anytime theDbSet
is iterated through orToList()
is called.