0

クラスのモックインスタンスでメソッドが特定の回数呼び出されるかどうかを確認しようとしています。問題は、メソッドにfunc delegateand があり、それが一致していないことです。

次のシナリオがあります。

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