`DefaultValueProvider` extensibility
Imagine I have
// Oversimplified
interface IContainer<T> {
T Value { get; set; }
}
Somewhere in my codebase. For unit-testing purposes I would also have a basic hand-mocked version:
class FakeContainer<T> : IContainer<T> {
public T Value { get; set; }
}
Now I want to be able to do stuff like this:
var mock = new Mock<IHaveIntegerContainer>();
mock.Object.IntegerContainer.Value = 42;
Assert.That(mock.Object.IntegerContainer.Value, Is.EqualTo(42));
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 custom DefaultValueProvider as follows:
public class ContainerAwareDefaultValueProvider : DefaultValueProvider {
protected override object GetDefaultValue(Type type, Mock mock) {
if (Utils.IsContainerType(type)) {
return Utils.MakeFakeContainer(type);
}
return new object(); // TODO What to return here?
}
}
I can even make a MockRepository instance in order to reuse this DefaultValueProvider implementation across the whole test suite. But question arises - what should I return in case the condition under the if evaluates to false (e.g. we're trying to instantiate an arbitrary type, unrelated to IContainer<T>)?
- I can't directly pass control to neither
DefaultValueProvider.Empty.GetDefaultValue(type, mock)norDefaultValueProvider.Mock.GetDefaultValue(type, mock), because the method isprotected internal - I can't inherit from neither
EmptyDefaultValueProvidernorMockDefaultValueProviderand callbase.GetDefaultValue(type, mock), because both classes aresealedwithinternalconstructors
Is my only option to duplicate the implementation of one of existing DefaultValueProviders? Or am I missing on the intended way to do what I'm seeking to do?
Due to lack of recent activity, this issue has been labeled as 'stale'. It will be closed if no further activity occurs within 30 more days. Any new comment will remove the label.
You can leverage UnsafeAccessorAttribute for this.
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 DefaultValueProvider, but it may be out of a desire to avoid leaking abstractions (i.e. MethodInfo and ParameterInfo from reflection) since those may not be the right ones down the road (i.e. in an AOT scenario).
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.
Due to lack of recent activity, this issue has been labeled as 'stale'. It will be closed if no further activity occurs within 30 more days. Any new comment will remove the label.
This issue will now be closed since it has been labeled 'stale' without activity for 30 days.
