开发者

Using dynamic with Unit Tests

开发者 https://www.devze.com 2023-02-20 18:04 出处:网络
I\'ve seen a few questions on here of people asking for criticism of their unit tests.I haven\'t seem them get closed, so I\'d like to do the same.

I've seen a few questions on here of people asking for criticism of their unit tests. I haven't seem them get closed, so I'd like to do the same.

I whipped up these tests, which I believe are made more readable by using dynamic, but I was wondering if anyone in the SO community had anything to add.

I know the use of dynamic is for some reason very controversial, and for some reason starts religious wars amongst C# developers. I'm really hoping to avoid that; I'm just trying to write some good tests 开发者_JS百科to help me do my job :)

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


Sure the use of dynamic reduces the lines of code you need to use. But think about what you want from unit tests first. You want them to tell you where your code goes wrong.

If one of the rows of data you add are wrong, will it tell you which one failed? And if one of the asserts fails, will it tell you which one?

So long as you can get the information you need, you should be fine. So it should tell exactly what went wrong and when it did.

0

精彩评论

暂无评论...
验证码 换一张
取 消