需要为单元测试选择断言

本文关键字:选择 断言 单元测试 | 更新日期: 2023-09-27 18:22:08

这是我的代码:

public int GetTotalIssuedCount()
{
    var storeCode = Store.Current.Code.ToLower();
    return (from i in Context.Instance.EduContainer.IssueDetailsSet
            where i.Status.ToLower() == "issued" && i.Store.Code == storeCode
            select i).Count();
}

这是我的测试代码:

[TestMethod]
public void GetTotalIssuedCountTest()
{
    StoreRepository sr = new StoreRepository();
    Assert.IsInstanceOfType();
}

哪种断言方法在这里合适?

需要为单元测试选择断言

在给定底层数据集状态的情况下,您需要断言count是否与您期望的相匹配:

[TestMethod]
public void GetTotalIssuedCountTest()
{
   // The 5 is exemplary value -
   // you need to determine actual one basing data set contents
   const int expectedIssuedCount = 5;
   var storeRepository = new StoreRepository();
   // Here you'll most likely need to prepare fake data set
   var actualIssuedCount = storeRepository.GetTotalIssuedCount();
   Assert.AreEqual(expectedIssuedCount, actualIssuedCount);
}

要使其工作,您需要设置fake数据集(EduContainer.IssueDetailsSet),您的方法将访问该数据集。您很可能需要mock依赖项注入来实现这一点。