ASP.NET MVC - Unit testing overkill? (TDD)

前端 未结 5 1181
误落风尘
误落风尘 2021-02-04 05:08

So I\'m starting to catch the TDD bug but I\'m wondering if I\'m really doing it right... I seem to be writing A LOT of tests.

The more tests the better

5条回答
  •  庸人自扰
    2021-02-04 05:52

    I'd say you are doing a little more than you probably have to. While it is nice to test every possible path your code can take, some paths just aren't very important or don't result in real differences in behavior.

    In your example take LogOn(string returnUrl)

    The first thing you do in there is check the returnUrl parameter and re-assign it to a default value if it is null/empty. Do you really need a whole unit test just to make sure that one line of code happens as expected? It isn't a line likely to break easily.

    Most changes that might break that line be things that would throw a compile error. A change in the default value being assigned is possible in that line (maybe you decide later that "/" isn't a good default value... but in your unit test, I bet you hard-coded it to check for "/" didn't you? So the change in the value will necessitates a change in your test... which means you aren't testing your behavior, instead you are testing your data.

    You can achieve a test for the behavior of the method by simply having one test that does NOT supply a parameter. That will hit your "set default" part of the routine while still testing that the rest of the code behaves well too.

提交回复
热议问题