单元测试的两种方式

发表于:2018-7-02 15:51

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:匠心十年    来源:博客园

  完整代码
  1   [TestClass]
  2   public class UnitTestTwoWays
  3   {
  4     public interface IShellSorter<T>
  5       where T : IComparable
  6     {
  7       void Sort(T[] list);
  8     }
  9
  10     public interface IBubbleSorter<T>
  11       where T : IComparable
  12     {
  13       void Sort(T[] list);
  14     }
  15
  16     public class ShellSorter<T> : IShellSorter<T>
  17       where T : IComparable
  18     {
  19       public void Sort(T[] list)
  20       {
  21         int inc;
  22
  23         for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
  24
  25         for (; inc > 0; inc /= 3)
  26         {
  27           for (int i = inc + 1; i <= list.Length; i += inc)
  28           {
  29             T t = list[i - 1];
  30             int j = i;
  31
  32             while ((j > inc) && (list[j - inc - 1].CompareTo(t) > 0))
  33             {
  34               list[j - 1] = list[j - inc - 1];
  35               j -= inc;
  36             }
  37
  38             list[j - 1] = t;
  39           }
  40         }
  41       }
  42     }
  43
  44     public class BubbleSorter<T> : IBubbleSorter<T>
  45       where T : IComparable
  46     {
  47       public void Sort(T[] list)
  48       {
  49         int i, j;
  50         bool done = false;
  51
  52         j = 1;
  53         while ((j < list.Length) && (!done))
  54         {
  55           done = true;
  56
  57           for (i = 0; i < list.Length - j; i++)
  58           {
  59             if (list[i].CompareTo(list[i + 1]) > 0)
  60             {
  61               done = false;
  62               T t = list[i];
  63               list[i] = list[i + 1];
  64               list[i + 1] = t;
  65             }
  66           }
  67
  68           j++;
  69         }
  70       }
  71     }
  72
  73     public interface INumberSorter
  74     {
  75       void Sort(int[] numbers);
  76     }
  77
  78     public class NumberSorter : INumberSorter
  79     {
  80       private IShellSorter<int> _shellSorter;
  81       private IBubbleSorter<int> _bubbleSorter;
  82
  83       public NumberSorter(
  84         IShellSorter<int> shellSorter,
  85         IBubbleSorter<int> bubbleSorter)
  86       {
  87         _shellSorter = shellSorter;
  88         _bubbleSorter = bubbleSorter;
  89       }
  90
  91       public void Sort(int[] numbers)
  92       {
  93         _bubbleSorter.Sort(numbers);
  94       }
  95     }
  96
  97     [TestMethod]
  98     public void TestSortNumberResult()
  99     {
  100       IShellSorter<int> shellSorter = new ShellSorter<int>();
  101       IBubbleSorter<int> bubbleSorter = new BubbleSorter<int>();
  102
  103       NumberSorter numberSorter = new NumberSorter(shellSorter, bubbleSorter);
  104       int[] numbers = new int[] { 3, 1, 2 };
  105       numberSorter.Sort(numbers);
  106
  107       // 验证返回值是否已经被正确排序。
  108       // 只要返回值正确即可,并不关心使用了哪个算法。
  109       CollectionAssert.AreEqual(new int[] { 1, 2, 3 }, numbers);
  110     }
  111
  112     [TestMethod]
  113     public void TestUseCorrectSortingAlgorithm()
  114     {
  115       IShellSorter<int> mockShellSorter = Substitute.For<IShellSorter<int>>();
  116       IBubbleSorter<int> mockBubbleSorter = Substitute.For<IBubbleSorter<int>>();
  117
  118       NumberSorter numberSorter = new NumberSorter(mockShellSorter, mockBubbleSorter);
  119       int[] numbers = new int[] { 3, 1, 2 };
  120       numberSorter.Sort(numbers);
  121
  122       // 验证排序器是否使用冒泡排序算法。
  123       // 如果排序器未使用冒泡排序算法,或者使用了该算法但传递了错误的参数,则验证失败。
  124       mockBubbleSorter.Received().Sort(Arg.Is<int[]>(numbers));
  125     }
  126   }
  

上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号