Testing a module using async / wait methods using NUnit

We recently updated our entire solution for Framework 4.5.2 and use async / wait. I have already written several tests for the data services layer and part of the business services layer, but now I have a new test that fails with NUnits "not all expected calls were made." We use NUnit V3.0.1 and NMock 2.0.0.

This is a proven method:

public async Task<objQuote> RetrieveQuoteAsync(int quoteid)
    {
        var q = await _data.RetrieveQuoteAsync(quoteid);
        if (q != null)
        {
            q.Milestones = await _data.RetrieveAllMilestonesForQuoteAsync(quoteid);
            q.Assignments = await _data.RetrieveAssignmentsForQuoteAsync(quoteid);
        }
        return q;
    }

And this is the definition for the mocking data class (_data in the above code):

public interface IQuotesData
{
    string ConnectionString { get; set; }


    Task<int> SaveQuoteAsync(objQuote quote);

    Task<objQuote> RetrieveQuoteAsync(int quoteid);

    //objQuote RetrieveQuoteWithoutAssignments(int quoteid);

    Task<List<objQuote>> RetrieveAllQuotesAsync();

    Task<List<objQuote>> RetrieveAllQuotesForFYAsync(int fy);

    Task<int> SaveQuoteActivityAsync(objQuoteActivity qa);

    Task DeleteQuoteAsync(int quoteid);

    Task<int> SaveQuoteMilestoneAsync(objQuoteMilestone ms);

    Task<List<objQuoteMilestone>> RetrieveAllMilestonesForQuoteAsync(int quoteid);

    Task<List<objQuoteActivity>> RetrieveAllQuoteActivitiesAsync(int quoteid);

    Task<int> SaveAssignmentAsync(objAssignment ass);

    Task<int> SaveAssignmentOverheadAsync(objFAOverHead oh);

    Task<List<objFAOverHead>> RetrieveAllOverheadsForAssignment(int assignmentid);

    Task<objAssignment> RetrieveAssignmentAsync(int assid);

    Task<List<objAssignment>> RetrieveAssignmentsForQuoteAsync(int quoteid);

    Task<int> SaveDelegationAsync(objDelegation del);

    Task<int> SaveDelegationOverheadAsync(objFAOverHead oh);

    Task<List<objFAOverHead>> RetrieveAllOverheadsForDelegation(int delegationid);

    Task<List<objDelegation>> RetrieveDelegationsforAssignment(int assid);

    Task<int> SaveCommentAsync(objQuoteComment comment);

    Task<List<objQuoteComment>> RetrieveAllCommentsForQuoteAsync(int quoteid);
}

And this is my test:

 [Test]
    public async void Can_Retrieve_Quote()
    {
        const int quoteid = 42;
        var quote = new objQuote() { ID = 42};
        var msList = new List<objQuoteMilestone>();
        var assignmentList = new List<objAssignment>();

        Expect.Once.On(_data).Method("RetrieveQuoteAsync").With(Is.EqualTo(quoteid)).Will(Return.Value(quote));
        Expect.Once.On(_data).Method("RetrieveAllMilestonesForQuoteAsync").With(Is.EqualTo(quoteid)).Will(Return.Value(msList));
        Expect.Once.On(_data).Method("RetrieveAssignmentsForQuoteAsync").With(Is.EqualTo(quoteid)).Will(Return.Value(assignmentList));

        var biz = new QuotesBiz(_data, _empData, _logger, _mailer);
        Assert.IsNotNull(biz);
        var results = await biz.RetrieveQuoteAsync(quoteid);
        Assert.That(results != null);

    }

At this point, I'm not sure if this is a coding problem or a testing problem. It seems that two calls inside the if if statement of the code being tested are not executed.

TIA to anyone who can help understand this.

+4
1

, mock, Task, .

NMock, , , .

, Task.FromResult :

[Test]
public async void Can_Retrieve_Quote()
{
    const int quoteid = 42;
    var quote = new objQuote() { ID = 42};
    var msList = new List<objQuoteMilestone>();
    var assignmentList = new List<objAssignment>();

    Expect.Once.On(_data).Method("RetrieveQuoteAsync").With(Is.EqualTo(quoteid)).Will(Return.Value(Task.FromResult(quote)));
    Expect.Once.On(_data).Method("RetrieveAllMilestonesForQuoteAsync").With(Is.EqualTo(quoteid)).Will(Return.Value(Task.FromResult(msList)));
    Expect.Once.On(_data).Method("RetrieveAssignmentsForQuoteAsync").With(Is.EqualTo(quoteid)).Will(Return.Value(Task.FromResult(assignmentList)));

    var biz = new QuotesBiz(_data, _empData, _logger, _mailer);
    Assert.IsNotNull(biz);
    var results = await biz.RetrieveQuoteAsync(quoteid);
    Assert.That(results != null);

}

, , , , .

0

Source: https://habr.com/ru/post/1629033/


All Articles