-
-
Notifications
You must be signed in to change notification settings - Fork 810
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
DefaultValueProvider
extensibility
#1450
Comments
Due to lack of recent activity, this issue has been labeled as 'stale'. |
You can leverage public class MyProvider(DefaultValueProvider inner) : DefaultValueProvider
{
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "GetDefaultValue")]
static extern object GetDefaultValueInner(DefaultValueProvider provider, Type type, Mock mock);
protected override object GetDefaultValue(Type type, Mock mock)
{
if (Utils.IsContainerType(type))
{
return Utils.MakeFakeContainer(type);
}
return GetDefaultValueInner(inner, type, mock);
}
} And this will pass: var mock = new Mock<IServiceProvider>(MockBehavior.Loose) { DefaultValue = DefaultValue.Mock };
mock.DefaultValueProvider = new MyProvider(mock.DefaultValueProvider);
Assert.NotNull(mock.Object.GetService(typeof(IFormatProvider))); I'd say this is a weird case, not being able to invoke any members of Feel free to close this issue if the above solution works, as I don't expect changes to the public API at this point for v4. |
Imagine I have
Somewhere in my codebase. For unit-testing purposes I would also have a basic hand-mocked version:
Now I want to be able to do stuff like this:
Which obviously throws NRE as-is, because
IntegerContainer
is null by default. If I understand the docs correctly, I can address this issue by implementing a customDefaultValueProvider
as follows:I can even make a
MockRepository
instance in order to reuse thisDefaultValueProvider
implementation across the whole test suite. But question arises - what should I return in case the condition under theif
evaluates to false (e.g. we're trying to instantiate an arbitrary type, unrelated toIContainer<T>
)?DefaultValueProvider.Empty.GetDefaultValue(type, mock)
norDefaultValueProvider.Mock.GetDefaultValue(type, mock)
, because the method isprotected internal
EmptyDefaultValueProvider
norMockDefaultValueProvider
and callbase.GetDefaultValue(type, mock)
, because both classes aresealed
withinternal
constructorsIs my only option to duplicate the implementation of one of existing
DefaultValueProvider
s? Or am I missing on the intended way to do what I'm seeking to do?The text was updated successfully, but these errors were encountered: