微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 使用NUnit使用异步/等待方法进行单元测试

我们最近更新了Framework 4.5.2的整个解决方案,并使用async / await.我已经为数据服务层和业务服务层的一部分编写了几个测试,但我现在有一个新的测试失败,NUnits“并非所有预期的调用都被执行”.我们使用的是NUnit V3.0.1和NMock 2.0.0.

这是测试中的方法

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;
    }

这是被模拟的数据类的定义(上面代码中的_data):

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);
}

这是我的测试:

[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);

    }

此时我不确定这是编码问题还是测试问题.看来,正在测试的代码的“if”语句中的两个调用没有被执行.

TIA对任何可以帮助解决这个问题的人都有帮助.

解决方法

从正在测试的方法中,您在模拟上调用方法期望返回一个Task,而mock返回具体对象.

我没有具体的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,_mailer);
    Assert.IsNotNull(biz);
    var results = await biz.RetrieveQuoteAsync(quoteid);
    Assert.That(results != null);

}

在将来,你可以通过调试测试来检查你的模拟返回的内容来捕获这些问题,在这种情况下你会看到null.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐