简体   繁体   English

如何为 void 方法编写单元测试并创建模拟数据集和数据行(数据集和数据行都是强类型)?

[英]How do I write a unit test for a void method and create a mock dataset and a datarow(Both dataset and datarow are strongly typed)?

I have a method whose return type is void.我有一个返回类型为 void 的方法。 This method has two parameters, a strongly typed datarow and dataset.该方法有两个参数,强类型数据行和数据集。

private void Check(Dataset testDB, DataRow testRow)
{

 if(testRow.Amount>10)
 {
    decimal rate = testRow.Rate;

    if(rate >= 100)
    {
      AddData(testDB,testRow,"some text");
    }

 }

}

private void AddData(Dataset testDB, DataRow testRow, string notes)
{
  Dataset.StudentRow newRow=testDB.Students.NewStudentRow();
  .
  .
  .
  testDB.Students.AddStudentRow(newRow);
}

You will not do any mocking.你不会做任何 mocking。 You will use real DataSet and DataRow and assert that data are correctly defined in data set after calling the method.您将使用真实的 DataSet 和 DataRow,并在调用该方法后断言数据在数据集中已正确定义。 You generally need several unit tests to cover the method:您通常需要几个单元测试来涵盖该方法:

  • Unit test with row having Amount 10行数为 10 的单元测试
  • Unit test with row having Amount 11 but rate 99单元测试,行的数量为 11,但评分为 99
  • Unit test with row having Amount 11 and rate 100单元测试,行的数量为 11,比率为 100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM