DEV Community

Discussion on: How do you like to unit test your code?

Collapse
 
sam_ferree profile image
Sam Ferree • Edited

I'm a huge fan of Test Driven Development, I've been working with Scott Hanselman's setup

So once I've done some experimenting and I know how I think the shape of my class/service/api will look, I define the interface, then I enter a test driven inner loop which consists of:

  1. Write test for a method (Red)
  2. Code the method until pass (Green)
  3. Refactor until happy (Refactor)
  4. Commit.

The same loop applies when I'm fixing bugs, Step 1 is to write an automated test that replicates the bug, then continues on as normal. Now with the setup described above, all of the building and testing happens automatically while I'm typing. so I'm writing code and red and green are just blinking, until I get to a point where I've got nothing but green.

I haven't managed to get this setup working slick testing parts of the app like controllers, but I'm still new at testing against views, so I imagine as I get more comfortable with it, I can get into a nice system.

I mock services to test business logic at the unit level, and use an in memory database to integration test my services both with and without business logic.