Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exercises to enforce good practices such as TDD and Mocking

Tags:

I'm looking for resources that provide an actual lesson plan or path to encourage and reinforce programming practices such as TDD and mocking. There are plenty of resources that show examples, but I'm looking for something that actually provides a progression that allows the concepts to be learned instead of forcing emulation.

My primary goal is speeding up the process for someone to understand the concepts behind TDD and actually be effective at implementing them. Are there any free resources like this?

like image 961
ramnik Avatar asked Jul 09 '09 18:07

ramnik


People also ask

What is TDD mocking?

Mocking is the creation of an object that mimics another and sets expectations concerning its behavior. In testing, mocking replicates the behavior of a service, object, or process.

What is the meaning of TDD?

Test-driven development (TDD), also called test-driven design, is a method of implementing software programming that interlaces unit testing, programming and refactoring on source code.


1 Answers

It's a difficult thing to encourage because it can be perceived (quite fairly) as a sea-change; not so much a progression to a goal but an entirely different approach to things.

The short-list of advice is:

  • You need to be the leader, you need to become proficient before you can convince others to, you need to be able to show others the path and settle their uncertainties.

  • First become proficient in writing unit tests yourself

    • Practice writing tests for existing methods. You'll probably beat your head on the desk trying to test lots of your code--it's not because testing is hard or you can't understand testing; it's more likely because your existing code and coding style isn't very testable.

    • If you have a hard time getting started then find the simplest methods you can and use them as a starting point.

  • Then focus on improving the testability of the code you produce

    • The single biggest tip: make things smaller and more to the point. This one is the big change--this is the hardest part to get yourself to do, and even harder to convince others of.

Personally I had my "moment of clarity" while reading Bob Martin's "Clean Code" book; an early chapter talks about what a clean method will look like and as an example he takes a ~40 line method that visually resembled something I'd produce and refactors it out into a class which is barely larger line-count wise but consists of nothing but bite-sized methods that are perhaps 3-7 lines each.

Looking at these itty-bitty methods it suddenly clicked that the unit-testing cornerstone "each test only tests one thing" is easiest to achieve when your methods only do one thing (and do that one thing without having 30 internal mechanisms at play).

The good thing is that you can begin to apply your findings immediately; practice writing small methods and small classes and testing along the way. You'll probably start out slow, and hit a few snags fairly quickly, but the first couple months will help get you pointed in the right direction.

like image 125
STW Avatar answered Nov 14 '22 07:11

STW