question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rework integration with Moq to support generic methods

See original GitHub issue

This issue is to track our activities regarding rework of Moq integration. Currently we are creating Mock<T> using reflection and then configure each method individually using reflection. Due to this approach we have a limitation and do not support generic methods, as we cannot configure all possible instantiations upfront.

We should change our integration approach and instead register AutoFixture as default value provider. This way we inject “passively” and are invoked by Moq when value is required. We are already following that way with NSubstitute and it works pretty well.

We haven’t investigated yet whether it’s possible, but @stakx promised to give it a look one day.

Let’s postpone this activity till the moment we start our work on v5, as this change might require us to change minimum supported version for Moq. Unless somebody wants to look upfront to prepare Moq for this.

I’ll update this issue once we start work on v5.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:13

github_iconTop GitHub Comments

2reactions
riezeboschcommented, Sep 23, 2019
    public class ValueProviderTests
    {
        [Fact]
        public void UseValueProvider()
        {
            var fixture = new Fixture();
            var mock = new Mock<IInterfaceWithGenericMethod>
            {
                DefaultValueProvider = new AutoFixtureValueProvider(fixture)
            };


            Assert.NotNull(mock.Object.GenericMethod<string>());
        }
        
        [Fact]
        public void CustomizedAnonymousType()
        {
            var fixture = new Fixture();
            fixture.Customize<string>(x => x.FromSeed(y => "asf"));
            var mock = new Mock<IInterfaceWithGenericMethod>
            {
                DefaultValueProvider = new AutoFixtureValueProvider(fixture)
            };


            Assert.Equal("asf", mock.Object.GenericMethod<string>());
        }

        public class AutoFixtureValueProvider : DefaultValueProvider
        {
            private readonly IFixture _fixture;

            public AutoFixtureValueProvider(IFixture fixture)
            {
                _fixture = fixture;
            }

            protected override object GetDefaultValue(Type type, Mock mock) =>
                _fixture.Create(type, new SpecimenContext(_fixture));
        }
    }

This looks promising, except for the fact the mock is still tight to the interface. However, I expect most of reflection code could be replaced by this.

1reaction
stakxcommented, Sep 23, 2019

@zvirja: Thanks for the reminder!

Someone noticed a while ago over at Moq’s repo that due to the accessibility of DefaultValueProvider’s methods, these providers aren’t as composable as they could be. I’ve been wanting to fix this in one of the next minor releases. It’ll likely be a very small breaking change. I thought I’d mention it here so that we can coordinate versions / release schedules if needed.

Also, if anyone here finds other problems with Moq’s DefaultValueProvider, please let me know.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking generic methods in Moq without specifying T
Yes, I get that, but I need the repo to support a few different types. It seems the only way to achieve this...
Read more >
How to test generic method with moq
I have a method that I want to test with moq: C#. public List<T> GetValidRecords<T>(Entities context) where T: class, IGetListOfTables { try ...
Read more >
ASP.NET Core Integration Testing & Mocking using Moq
Mocking services using Moq while integration testing ASP. ... The ConfigureServices and Configure methods in your applications Startup class ...
Read more >
Generic methods - Unit Testing in C#
When dealing with dependencies exposing generic methods, developers can use Moq to configure these methods accordingly by placing constraints on the incoming ...
Read more >
Using Callbacks with Moq
A callback is a piece of code that is passed into to a method parameter to be executed from within that method. Callbacks...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found