-
-
Notifications
You must be signed in to change notification settings - Fork 803
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 custom default value generators (i.e. make default value providers part of the public API) #533
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This refactoring will pay off later because it allows us to extract `DefineDefault` from `IDefaultValueProvider` and move it into `Mock`. This in turn will make it possible to turn default value providers into singletons.
by removing `IDefaultValueProvider.DefineDefault` and implementing the same functionality directly in the `Mock.GetDefaultValue` method added earlier.
This commit also includes a small breaking change: `Mock.DefaultValue` is no longer virtual, as there is now only one possible correct way of implementing it: deriving its value from `Mock.DefaultValueProvider` and updating that same other property in the setter.
First, if the default value provider mechanism is to be more generic in nature, it should be possible for custom providers to create "inner mocks" without having access to `Mock` internals. This commit makes it possible for any default value provider to participate in This mechanism simply by returning a mocked object. Second, this refactoring is prerequisite to changing the `Provide- Default` method's signature (so it accepts a `Type` instead of a `MethodInfo`).
This commit replaces the `IDefaultValueProvider` interface with a new abstract base class `DefaultValueProvider`, which is meant to eventu- ally become part of the public API.
Originally, those methods without the suffix were supposed to be the ones performing parameter validation. However, Moq doesn't actually call those. So instead let's remove the validating methods and give the "clean" method names to the protected methods that are overridden in derived classes. Also, remove unit tests associated with the validation that no longer happens. Add `Debug.Assert`s to document the new (implicit) contract.
* Move `Mock.configuredDefaultValues` field to `Mock<T>` * Fix whitespace
This was referenced Nov 25, 2017
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
This is a follow-up to #366, where I said:
Almost half a year later, I'm back with this PR. It allows implementation of custom default value providers:
Which is massively simpler than what the earlier PR proposed. A mock—or mock repository—can then be set up to use that provider as follows:
(For backwards compatibility and ease of use,
Mock.DefaultValue
can still be set toDefaultValue.Empty
orDefaultValue.Mock
.)This is meant to be both an extension point for better interoperation with libraries such as AutoFixture, and as a potential prerequisite for resolving #173 and #330 (which ask for functional changes regarding
DefaultValue.Empty
, which possibly means its behavior should be configurable in order to not introduce breaking changes).