Question

I'm learning to do TDD in practice in small project. I want to create a countdown timer class, how to implement it in TDD(Red, Green, Refactor), and it has the delegate callback as well.

Was it helpful?

Solution

I am not sure of your exact question, but I think you want to know how you can write tests first for your countdown timer?

If so start with writing one of the primary tests, say Start, Stop, or Reset. These are your action events and will be the ones you invoke first (most likely). Code these tests first and execute them. They should be red because they can't actually run the countdown timer.

Then add dummy code to one of the functions in your countdown timer. For instance, add the Start() function which starts the countdown timer. Leave the function empty for now. The test should still be red

Now we need to add a way to get the current time remaining. Add a "Remaining" property where we can check the time remaining. Update the test so that it captures this property, starts the timer and then checks the property again to see if it changed. The test should be red still since we have no code to change the property.

Update the start method to kick off your countdown process and update the internals of your countdown timer. Rerun your test now and it should be green since the value is changing.

Now it's time to refactor. Look through your code and clean things up. Simplify where you can and then reexecute your tests. If all is good everything should be green and you can move on to your next test.

Hope that helps.

OTHER TIPS

Generally I don't test things like that in unit tests. Instead, I'll keep that code as small as possible, and have it call the class that does the work with either the time I want it to be, the elapsed time, or just a general "Do work now" kind of message.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top