Taswar Bhatti
Practical guides on AI agents, LLM security, C#/.NET and Cloud, from Taswar Bhatti
Tag: unit testing
serviceLocator

There are certainly many ways to stub out dependency, using constructor injection, etc etc. Here is one way to quickly stub out ServiceLocator rather than implementing ServiceLocatorStub.

Lets say you have code that uses a helper and the dependency is not quite the responsibility of that object but its the sub object that uses it. As an example the IAccessHelper uses some security server to check some values. But the AccountController doesnt use it so why inject security service in AccountController? In that case you will call a service locator to get you the AccessHelper object.

… Read the rest “Stubbing out ServiceLocator for unit testing”
Working Effectively with Legacy Code

In this Learn Series I am going to write about the Sprout Method which is in the book “Working Effectively with Legacy Code by Michael Feathers“, all I have to say is what a great book if you don’t have it, you should pick it up asap.

Sprout Method is a technique that one can use when one has to write a new feature into a system. The code will be formulated as new code, and you want to put the new code into a new method and call it where the new functionality needs to be.

The reason for Sprout method is that sometimes you cannot write unit test for the code that you want to change but you can use Test Driven Development to write unit test for the new code, so that at least the … Read the rest “Learn The Sprout Method for adding new functionality”

Working Effectively with Legacy Code

This is part 2 of Extract and Override refactoring technique. You can find part 1 in my previous post.

I am going to show another technique by using Factory Method to break dependency in your code.

*WARNING* *WARNING*

Before I begin I would like to say all these techniques are not the best design but they will help you to break dependency and it might make some people flinch for the method but the intention is to get people to start looking at breaking dependency. Hopefully it will lead them to learn better design and make their code more modular in the process of it.

For anyone who doesn’t know Factory Method, you can read the GoF book or Head First Design Patterns. Which I would suggest you to buy if you don’t already own them.

Let’s say you … Read the rest “Extract and Override refactoring technique part 2”

artofunittesting

Resources

  • Manning – Where one can buy the MEAP early release of the book
  • Amazon – Sometimes cheaper than Manning for print books
  • Art of Unit Testing Wiki Site -The wiki site for the book
  • The book will be in print I think April 2009 as Roy mentioned in his blog if you wish to wait for a hard copy

 

Additional Information

  • Paperback: 225 pages
  • Publisher: Manning Publications (May 1 2009)
  • Language: English
  • ISBN-10: 1933988274
  • ISBN-13: 978-1933988276

 

Audience and Content

The intended audience for this book are people who are beginning/intermediate learning unit testing or in the process of implementing unit testing at their shops. The contents is for developers to start writing better unit test. I wouldn’t say it is a book target at expert unit testers out there, people who may already be using SpecUnit or … Read the rest “Book Review: The Art of Unit Testing by Roy Osherove”

Working Effectively with Legacy Code

When doing unit testing, one always comes to a point where there this “kind of hard” part to test since the part depends on something you might not have control over or depends on the functionality of the dependency to complete.

In “Working Effectively with Legacy Code” by Michael C. Feathers talks about a dependency breaking technique called “Extract and Override” which is also in Roy Osherove book “Art of Unit Testing“.

The basic technique is quite simple and quite powerful as Roy puts it

“Extract & Override is a very powerful technique because it lets you deal directly with replacing the dependency without going down the rabbit hole at all (changing dependencies deep inside the call stack). That makes it very quick and clean to perform, almost to the point … Read the rest “Extract and Override refactoring technique”

UA-4524639-2