Using TDD: “top down” vs. “bottom up”

2020-05-17 06:57发布

Since I'm a TDD newbie, I'm currently developing a tiny C# console application in order to practice (because practice makes perfect, right?). I started by making a simple sketchup of how the application could be organized (class-wise) and started developing all domain classes that I could identify, one by one (test first, of course).

In the end, the classes have to be integrated together in order to make the application runnable, i.e. placing necessary code in the Main method which calls the necessary logic. However, I don't see how I can do this last integration step in a "test first" manner.

I suppose I wouldn't be having these issues had I used a "top down" approach. The question is: how would I do that? Should I have started by testing the Main() method?

If anyone could give me some pointers, it will be much appreciated.

标签: tdd
7条回答
贪生不怕死
2楼-- · 2020-05-17 07:57

The main should be very simple in the end. I don't know how it looks like in c#, but in c++ it should look something like this :

#include "something"

int main( int argc, char *argv[])
{
  return TaskClass::Run( argc, argv );
}

Pass already created objects to the constructors of classes, but in unit tests pass mock objects.

For more info about TDD, take a look at these screencasts. They explain how to do agile development, but it also talks how to do TDD, with examples in c#.

查看更多
登录 后发表回答