How to start unit testing or TDD?

后端 未结 14 1839
离开以前
离开以前 2020-12-04 07:18

I read a lot of posts that convinced me I should start writing unit test, I also started to use dependency injection (Unity) for the sake of easier mocking, but I\'m still n

14条回答
  •  既然无缘
    2020-12-04 07:43

    Steve Sanderson has a great writeup on TDD best practices.

    http://feeds.codeville.net/~r/SteveCodeville/~3/DWmOM3O0M2s/

    Also, there's a great set of tutorials for doing an ASP.net mvc project that discusses a lot TDD principles (if you don't mind learning ASP.net MVC along the way) http://www.asp.net/learn/mvc-videos/ Look for the "Storefront" series at the bottom of the page.

    MOQ seems to be the hot mocking framework lately, you may want to look into that as well

    In summary, try to write a test to validate something you'r trying to archive, then implement the code to make it work.

    The pattern is known as Red - Green - Refactor. Also do your best to minimize dependencies so that your tests can focus on one component.

    Personally, I use Visual Studio Unit Tests. I'm not a hardcore TDD developer, but what i like to do is this:

    1. Create a new project and define a few of the fundamental classes based on the system design (that way I can at least get some intellisense)
    2. create a unit tests project and start writing unit tests to satisfy the functionality i'm trying to implement.
    3. Make them fail
    4. Make them pass (implement)
    5. Refactor
    6. Repeat, try to make the test more stringent or create more tests until i feel its solid.

    I also feel its very useful to add functionality onto an exiting code base. If you want to add some new feature, first create the unit test for what you want to add, step through the code to see what you have to change, then go through the TDD process.

提交回复
热议问题