使用动态单元测试

Ada*_*kis 7 c# unit-testing dynamic

我在这里看到了一些人要求批评他们的单元测试的问题.我似乎没有被关闭,所以我想做同样的事情.

我掀起了这些测试,我认为这些测试通过使用更具可读性dynamic,但我想知道SO社区中是否有任何人需要添加.

我知道动态的使用出于某种原因非常有争议,并且由于某种原因在C#开发者之间开始宗教战争.我真的希望避免这种情况; 我只是想写一些好的测试来帮助我完成我的工作:)

    [TestMethod]
    public void TestAllocation() {
        SearchView.StubPropertyNumValueThenSetUpSearchView<WellDetail>("TX", Property.WorkingInterestTaxSubtypeId);
        Presenter.SetUpPhaseAndFmvValues(Phase.PhaseIdForForRenderAppraiser, 1000);

        AddTheseItems(
            new { PropNum = "pn1", CAN = "can1", MostRecentFmv = 10 },
            new { PropNum = "pn1", CAN = "can1", MostRecentFmv = 10 },
            new { PropNum = "pn1", CAN = "can1", MostRecentFmv = 10 },
            new { PropNum = "pn1", CAN = "can1", MostRecentFmv = 10 },

            new { PropNum = "pn1", CAN = "can2", MostRecentFmv = 40 },
            new { PropNum = "pn1", CAN = "can2", MostRecentFmv = 40 },
            new { PropNum = "pn1", CAN = "can2", MostRecentFmv = 40 },

            new { PropNum = "pn2", CAN = "can1", MostRecentFmv = 50 },
            new { PropNum = "pn2", CAN = "can1", MostRecentFmv = 50 });

        Presenter.Process(SearchView, ItemsToProcess);

        AssertTheseItemsExist(
            new { NumberOfTimes = 4, PropNum = "pn1", CAN = "can1", FmvCalculated = 100 },
            new { NumberOfTimes = 3, PropNum = "pn1", CAN = "can2", FmvCalculated = 400 },
            new { NumberOfTimes = 2, PropNum = "pn2", CAN = "can1", FmvCalculated = 500 });
    }

    private void AddTheseItems(params dynamic[] MassUpdateDtos) {
        foreach(dynamic item in MassUpdateDtos)
            ItemsToProcess.Add(new MassFMVUpdateDTO(new WellDetail() { PropertyNum = item.PropNum, CountyAccountNum = item.CAN }, new FMVHistory(), 0, item.MostRecentFmv));
    }

    private void AssertTheseItemsExist(params dynamic[] uniqueTargets) {
        foreach (dynamic target in uniqueTargets)
            Assert.AreEqual(target.NumberOfTimes, ItemsToProcess.Count(f => f.PropertyNum == target.PropNum && f.CountyAccountNum == target.CAN && f.FMV == target.FmvCalculated));
    }
Run Code Online (Sandbox Code Playgroud)

MrF*_*Fox 2

当然,使用动态可以减少您需要使用的代码行数。但首先考虑一下你想从单元测试中得到什么。您希望他们告诉您代码哪里出了问题。

如果你添加的其中一行数据是错误的,它会告诉你哪一行失败了吗?如果其中一项断言失败,它会告诉您是哪一项吗?

只要您能够获得所需的信息,就应该没问题。因此,它应该准确地说明出了什么问题以及何时发生的。