Skip to content

Functional regression in Moq.AutoMock 4.0.0 #450

@nil4

Description

@nil4

A regression is observed upgrading a project from v3.6.1 to v4.0.0 -- code that worked fine with the earlier version throws unexpectedly with the new one.

Steps to reproduce

> dotnet --version
10.0.201

> dotnet new console
> dotnet package add Microsoft.Extensions.Options
> dotnet package add Moq.AutoMock --version 3.6.1

Replace Program.cs with:

using Microsoft.Extensions.Options;
using Moq;
using Moq.AutoMock;

static class Program
{
    static void Main()
    {
        var mocker = new AutoMocker();
     
        var options = mocker.GetMock<IOptions<TestOptions>>();
        options.SetupGet(x => x.Value).Returns(new TestOptions());

        var serviceProvider = mocker.GetMock<IServiceProvider>();
        serviceProvider.Setup(x => x.GetService(It.IsAny<Type>()))
            .Returns<Type>((serviceType) => mocker.GetMock(serviceType).Object);
    }

    sealed class TestOptions;
}

Run the program and observe that it builds and executes without issues.

Upgrade the Moq.AutoMock package to version 4.0.0 and run the program again.

Expected behavior

Mocking behaves consistently with the earlier version.

Observed behavior

Unhandled exception. System.ArgumentException: Registered service `Moq.AutoMock.AutoMocker` was not a mock
   at Moq.AutoMock.AutoMocker.GetMockImplementation(Type serviceType, Boolean enablePrivate)
   at Moq.AutoMock.AutoMocker.GetMock(Type serviceType, Boolean enablePrivate)
   at Moq.AutoMock.AutoMocker.GetMock(Type serviceType)
   at Moq.AutoMock.AutoMocker.GetMock[TService]()
   at Program.Main() in Program.cs:line 14

Is this a breaking change in version 4.0.0? I couldn't find change logs or release notes for v4, so it's possible that I had overlooked something relevant. Would appreciate any insight into addressing this new behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions