0

我正在尝试检查一个方法是否在类的模拟实例上被调用了特定次数。问题是该方法有一个func delegate并且不匹配。

我有以下情况:

public interface ISomeService: IService
{
    Task CleanupMethod(CancellationToken cancellationToken);
}

public interface I
{
    Task invokedMethod(string aName, Func<IService, Task> action);
}


public class ClassGoingToBeUnitTested
{
    // instance of I
    private I instanceOfI;

    // a list of names.
    private static readonly string[] serviceNames =
    {
        "Name1",
        "Name2"
    };

    // constructor
    public ClassGoingToBeUnitTested(I passedInstance)
    {
        this.instanceOfI = passedInstance;
    }


    public void methodToBeUnitTested(object cancellationToken)
    {
        // my logic here

        // here I am calling invokedMethod method to known number of times.
        // something like this.

        try
        {
            IEnumerable<Task> someTasks = serviceNames.Select(
                name => this.instanceOfI.invokedMethod(
                    name,
                    service => ((ISomeService)service).CleanupMethod((CancellationToken)cancellationToken)
                    ));

            // here I run the tasks
            Task.WaitAll(someTasks.ToArray());

        }
        catch
        {
            // proper catching of exceptions
        }

        // other logic
    }
}


[TestClass]
public class ClassGoingToBeUnitTestedTest
{
    // mock of I interface
    private I IMock;

    // ClassGoingToBeUnitTested object
    ClassGoingToBeUnitTested classGoingToBeUnitTested;

    [TestInitialize]
    public void init()
    {
        this.IMock = Substitute.For<I>();
        this.classGoingToBeUnitTested = new ClassGoingToBeUnitTested(this.IMock);
    }


    [TestMethod]
    public void methodToBeUnitTested_Success()
    {
        // Arrange
        var cancellationTokenSource = new CancellationTokenSource();

        // Act
        this.classGoingToBeUnitTested.methodToBeUnitTested(cancellationTokenSource.Token);

        // Assert
        // this is throwing exception.
        this.IMock.Received(1).invokedMethod(
             "Name1",
             service => ((ISomeService)service).CleanupMethod((CancellationToken)cancellationTokenSource.Token)); // problem lies in this line.

    }
}

在上面的代码中,如果我更改((ISomeService)service).CleanupMethod((CancellationToken)cancellationTokenSource.Token))Arg.Any<Func<IService, Task>(),它会完美运行。但我不想检查我的用例。

到目前为止,我已经能够调试参数匹配器通过引用匹配委托,因此无法正确匹配参数。但我无法正确匹配论点。

我也尝试调用委托,但没有成功。我想我错过了一些东西。任何帮助将不胜感激。

4

1 回答 1

1

我通过使用解决了这个问题Invoke。我首先模拟了每当调用模拟对象时invokedMethod调用模拟对象的行为,serviceInstanceMock然后检查自身CleanupMethod调用的次数serviceInstanceMock

[TestClass]
public class ClassGoingToBeUnitTestedTest
{
    // mock of I interface
    private I IMock;

    // mock of ISomeService
    private ISomeService someServiceMockInstance;

    // ClassGoingToBeUnitTested object
    ClassGoingToBeUnitTested classGoingToBeUnitTested;

    [TestInitialize]
    public void init()
    {
        this.IMock = Substitute.For<I>();
        this.ISomeService = Substitute.For<ISomeService>();
        this.classGoingToBeUnitTested = new ClassGoingToBeUnitTested(this.IMock);
    }


    [TestMethod]
    public void methodToBeUnitTested_Success()
    {
        // Arrange
        var cancellationTokenSource = new CancellationTokenSource();
        this.IMock.invokedMethod(
            Arg.Any<string>,
            Arg.Do<Func<IService, Task>(x => x.Invoke(this.someServiceMockInstance)));

        // Act
        this.classGoingToBeUnitTested.methodToBeUnitTested(cancellationTokenSource.Token);

        // Assert
        this.IMock.Received(1).invokedMethod(
             "Name1",
             Arg.Any<Func<IService, Task>()); // changed this

        this.IMock.Received(1).invokedMethod(
             "Name2",
             Arg.Any<Func<IService, Task>()); // added this as well

        // adding to check the Received call on the service instance
        this.someServiceMockInstance.Received(2).CleanupMethod(cancellationTokenSource.Token);
    }
}
于 2019-12-02T11:40:47.417 回答