通过一个例子来理解Stub的概念

时间:2018-01-21 12:34:21

标签: c# unit-testing nunit moq

我最近收到了一些关于我们很久以前从承包商为遗留应用程序编写的一些测试的反馈。我对他使用的术语感到困惑,我再也没有机会再与他交谈一个月(当我会和他说话时)。请参阅以下代码:

 [TestMethod]
        public void Is_Correct_Months()
        {
            IService service = new Service();

            DataTable t1 = service.GetMonths();
            DataTable t2 = BuildExpectedTable();

            Assert.IsNotNull(t1);
            Assert.AreEqual(t1.Rows.Count, t2.Rows.Count);

            for (int i = 0; i <= 2; i++)
            {
                Assert.AreEqual(t1.Rows[i][0], t2.Rows[i][0]);
                Assert.AreEqual(t1.Rows[i][1], t2.Rows[i][1]);
            }
        }

        public DataTable BuildExpectedTable()
        {
            DataTable dt = new DataTable();
            //Add column names to datatable
            dt.Columns.Add("Column1", typeof(string));
            dt.Rows.Add("January");
            dt.Rows.Add("May");
            dt.Rows.Add("December");
            return dt;
        }

问题:在这种情况下,BuildExpectedTable方法是否为Stub?对BuildExpectedTable进行断言是否是一个坏主意(正如我所做的那样)?

现在看下面的代码:

 var mockCalculator = new Moq.Mock<ICalculator>();
            mockChangeCalculator.SetupProperty(client => client.Number1, 400).SetupProperty(client => client.Number2, 200);

这显然是一种嘲弄。 问题:它是否会被解释为Stub?

现在看下面的代码:

int[] values = new int[] { 1, 3, 5, 9 };
 
    IEnumerable<int> processed = ProcessInput(values);
 
    int[] expected = new int[] { 1, 9, 25, 81 };
    CollectionAssert.AreEqual(expected, processed);

问题:在这种情况下,预期存根吗?

更新

[TestMethod]
            public void Is_Correct_Months()
            {
                IService service = new Service();

                DataTable t1 = service.GetMonths();

                DataTable dt2 = new DataTable();
                //Add column names to datatable
                dt.Columns.Add("Column1", typeof(string));
                dt.Rows.Add("January");
                dt.Rows.Add("May");
                dt.Rows.Add("December");

                Assert.IsNotNull(t1);
                Assert.AreEqual(t1.Rows.Count, t2.Rows.Count);

                for (int i = 0; i <= 2; i++)
                {
                    Assert.AreEqual(t1.Rows[i][0], t2.Rows[i][0]);
                    Assert.AreEqual(t1.Rows[i][1], t2.Rows[i][1]);
                }
            }

0 个答案:

没有答案