Unplanned
Last Updated: 30 May 2019 08:38 by ADMIN
Robert
Created on: 23 May 2019 15:05
Type: Feature Request
0
How do I assert that "await someTask<T>" has been called

Let's have the following class and unit test:

public delegate Task<int> SomeDelegate();

public class Class1
{
    public async Task<int> ExecuteAsync(SomeDelegate next)
    {
        Task<int> task1 = next.Invoke();
        int int1 = await DoSomeStuff();
        int int2 = await task1;
        return int1 + int2;
    }

    private async Task<int> DoSomeStuff()
    {
        await Task.Delay(100);
        return 1;
    }
}

[TestMethod]
public async Task TestMethod1()
{
    // Arrange
    SomeDelegate next = Mock.Create<SomeDelegate>();
    next.Arrange(n => n.Invoke()).TaskResult(2).OccursOnce();
    // Act
    int sum = await new Class1().ExecuteAsync(next);

    // Assert
    Mock.Assert(next);
    Assert.AreEqual(3, sum);
}

JustMock public API lacks of convenient way to assert that particular task has been awaited. Potential workaround involves some "insider knowledge" that awaiting a Task internally results in a calls to some of its members.

0 comments