7

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 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));
    }
10
  • What's the benefit of using dynamic in your case? Commented Mar 29, 2011 at 14:35
  • Overall I find not problem with this and I lean anti-dynamic. The question I would ask is, did you really save anything by not creating a little private class with auto properties for PropNum, CAN, etc.? You still are tightly coupled between the creation of the anonymous types and their usage and the compiler won't help you out. If your good with that, and your team is good with that then go for it. Commented Mar 29, 2011 at 14:38
  • 2
    @Daniel I think it just makes the tests a lot more readable; it focuses them on the particular data pieces that are relevant. The AddTheseItems method shows how bloated the test would be if I were to use the proper, statically typed objects (IMO). Commented Mar 29, 2011 at 14:38
  • 1
    @Adam: Your AddTheseItems would only be longer by the name of your class: new Params { PropNum = "pn1", CAN = "can1", MostRecentFmv = 10 }, Commented Mar 29, 2011 at 14:40
  • 1
    @Daniel - am I correct in assuming that the intended use of dynamic was an alternative to reflection, and for comm interop? Commented Mar 29, 2011 at 14:53

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 That's a good point. From what I can tell neither how I have it now, nor how it would be if I added in simple classes to avoid dynamic would tell me specifically which row caused the failure. For me I expect the test to pass far, far more than I expect it to fail, so I figure I can just step into the test if I ever refactor my code such that the test breaks. Does that sound like decent reasoning?
Exactly, you write the unit tests too detect bugs, when they do that you want them to show you where the bug is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.