a bit of an abstract question about Test Driven Development

test driven development , tdd , csharp Santo Domingo, Dominican Republic
  • 12 years ago
    Greetings, I'm hoping that will your reply, i'll get into a better mindset for TDD... for the past months, i've been doing a lot of unit testing to existing solutions, I have a somewhat decent understanding of unit-testing and conceptually I understand that TDD will help me be more organized and ultimately a better developer. My biggest problem however is that I don't know how to really start. Usually I create a project skeleton with different layers (Service Repository, a DTO layer, a Web UI Layer and a Utilities layer), and one test project... I then start writing my first test... let's say for the argument, i'm building a movie catalog. the first story in my mind is that I need to list movies in a grid. my first test is that I need to have a movie object I can use, so I do this and then create class... and then I get stuck, how is it that I have to think so that i drives my design? this is the part i'm getting stuck in, the first tests if you will. Any help/comment will be appreciated. Thanks
  • 11 years ago
    Designing software to be testable is a bit of a mental exercise, one way it is done is through a technique called Inversion of Control (IOC). Martin Fowler has written extensively on this topic. When designing a new class, think about what dependancies it has, some kind of objects it will need to use to do its job and pass them in via the constructor. I also like to use a framework called Moq (pronounced Mock-you, http://code.google.com/p/moq/) for mocking objects during unit tests, but others are available (RhinoMocks). An example using the movie catalog analogy is that you want to have a class that counts the number of movies you have by a certain director. What you want to test is the object's ability to perform its business logic, if it uses other objects to do this, those other objects should have their own tests, so you dont need to test them, you can mock them out and write a test that says "assuming this other thing works, if I ask for X and it returns Y, my object's method should return Z" It works something like this: public interface IService { string[] GetMovieNamesByDirector(string director); string[] GetMovieNamesWithActor(string actor); } public class MovieCounter { private IService mService; private MovieCounter() { } public MovieCounter(IService service) { mService = service; } public int CountMoviesWithCredit(string name) { int counter = 0; counter += mService.GetMovieNamesByDirector(name).Length; counter += mService.GetMovieNamesWithActor(name).Length; return counter; } } and now the test (using Moq and NUnit) [TestFixture] public class MovieCounterTests { [Test] public void ShouldCountMovies() { Mock mockService = new Mock(MockBehavior.Strict); mockService.Expect(x => x.GetMovieNamesByDirector("Robert Rodriguez")).Returns(new string[] { "El Mariachi", "Once Uppon a Time In Mexico" }).Verifiable(); mockService.Expect(x => x.GetMovieNamesWithActor("Robert Rodriguez")).Returns(new string[]{}).Verifiable(); mockService.Expect(x => x.GetMovieNamesByDirector("Samuel Jackson")).Returns(new string[] {}).Verifiable(); mockService.Expect(x => x.GetMovieNamesWithActor("Samuel Jackson")).Returns(new string[] { "Pulp Fiction" }).Verifiable(); MovieCounter mc = new MovieCounter(mockService.Object); Assert.AreEqual(2, mc.CountMoviesWithCredit("Robert Rodriguez")); Assert.AreEqual(1, mc.CountMoviesWithCredit("Samuel Jackson")); mockService.Verify(); } } The final verify() call is used to make sure that you aren't expecting that your object makes additional calls to the mocked object that dont get made. If you expect 3 methods to get called in the course of execution and in reality only two get called, all of your assertions may work but you will fail the verify since a method didn't get called. Maybe it isnt needed anymore, maybe it can get refactored out. I never built a real implementation of IService, but I know that when one is built my class will work properly.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In theory, theory and practice are the same. In practice, they're not.”