Using dynamic with unit tests

I saw several questions about people who asked to criticize their unit tests. It seems to me that they do not close, so I would like to do the same.

I got enraged by these tests, which I think have become more readable using dynamic , but I was wondering if anyone from the SO community has anything to add.

I know that the use of dynamics for some reason is very controversial and for some reason starts religious wars among C # developers. I really hope to avoid this; I'm just trying to write some good tests 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)); } 
+7
source share
1 answer

I am sure that using dynamic shortens the lines of code that you need to use. But first think about what you want from unit tests. You want them to tell you where your code is going wrong.

If one of the rows of data that you add is erroneous, will you be informed which of them failed? And if one of the statements fails, will it tell you which one?

As long as you can get the information you need, you should be fine. Therefore, he must say for sure what went wrong and when it happened.

+2
source

All Articles