简体   繁体   中英

How does Moq verify Generic Func Parameter?

Given

public interface IService
{
  void Run<T>(T value, Func<T, string> func);
}

public class Sut
{
  private readonly IService m_service;

    public Sut(IService service)
    {
        m_service = service;
    }

    public void Test()
    {
        Point point = new Point {X = 1, Y = 2};
        m_service.Run(point, p => $"X:{p.X}, Y:{p.Y}");
    }
}

internal class Point
{
    public int X { get; set; }
    public int Y { get; set; }
}

And tested like

[Test]
public void RunTest()
{
    // Arrange
    Mock<IService> mockService = new Mock<IService>();
    var sut = new Sut(mockService.Object);

    // Act
    sut.Test();

    // Assert
    mockService.Verify(e => e.Run(It.IsAny<It.IsAnyType>(), It.IsAny<Func<It.IsAnyType, string>>()), Times.Once);
}

This test will failed, due to Point is third party internal class, I can not use It.IsAny<Point> in the verify.

if I change the Point to the public and verify use following, it's working good.

mockService.Verify(e => e.Run(It.IsAny<Point>(), It.IsAny<Func<Point, string>>()), Times.Once);

any help would be appreciated

Generally, you want to put an InternalsVisibleTo attribute in the assembly under test, pointing to the test assembly.

See here for reference information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM