Skip to content
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

DefaultValueProvider extensibility #1450

Open
eranikid opened this issue Dec 19, 2023 · 2 comments
Open

DefaultValueProvider extensibility #1450

eranikid opened this issue Dec 19, 2023 · 2 comments

Comments

@eranikid
Copy link

eranikid commented Dec 19, 2023

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) nor DefaultValueProvider.Mock.GetDefaultValue(type, mock), because the method is protected internal
  • I can't inherit from neither EmptyDefaultValueProvider nor MockDefaultValueProvider and call base.GetDefaultValue(type, mock), because both classes are sealed with internal constructors

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?

Back this issue
Back this issue

Copy link

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.

@github-actions github-actions bot added the stale label Aug 24, 2024
@github-actions github-actions bot removed the stale label Sep 3, 2024
@kzu
Copy link
Member

kzu commented Sep 3, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants