需要选择断言进行单元测试吗?

时间:2013-07-13 12:37:13

标签: c# asp.net asp.net-mvc unit-testing

这是我的代码:

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

哪种断言方法适用于此?

1 个答案:

答案 0 :(得分:2)

在给定基础数据集状态的情况下,您需要断言计数是否符合您的预期:

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

要使其正常工作,您需要设置您的方法将访问的数据集(EduContainer.IssueDetailsSet)。您很可能需要mocksdependency injection来实现这一目标。