Autofac.Extras.Moq icon indicating copy to clipboard operation
Autofac.Extras.Moq copied to clipboard

AutoMock fails creating a mock for a delegate type

Open olstakh opened this issue 5 months ago • 0 comments

Describe the Bug

Assuming AutoMock should support Mock's functionalities - AutoMock fails to create a mock object for a delegate type

Steps to Reproduce

    public interface MyInterface
    {
        delegate int MyDelegate(int x);
    }

public class ReproTest
{
  [Fact]
  public void Repro()
  {
        var autoMock = AutoMock.GetLoose().Mock<MyInterface.MyDelegate>(); // FAIL, unable to cast exception
        var mock = new Mock<MyInterface.MyDelegate>(); // OK
        mock.Setup(x => x(It.IsAny<int>())).Returns(20); // OK
  }
}

Expected Behavior

Successful creation of an auto mock from a delegate type

Exception with Stack Trace

Unable to cast object of type 'MyDelegate' to type 'Moq.IMocked`1[MyInterface+MyDelegate]'.

Happens in AutoMock.Mock method, when casting to IMocked<T>

var obj = (IMocked<T>)Create<T>(true, parameters);

Dependency Versions

Autofac: 6.1.1.0

olstakh avatar Sep 25 '24 22:09 olstakh