X

Stubbing out ServiceLocator for 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.


public class AccountController: Controller
{    
     public bool DoSomething()
     {
          var accessHelper = ServiceLocator.Current.GetInstance();

           if(accessHelper.IsValid(user))
            {
                //do something
                return true;
            }

            return false;
     }

}

Now if you wish to unit test this you will need to do quite a few things to stub it out. The quick way to do this is using property in C#

public class AccountController: Controller
{    
     public boolean DoSomething()
     {

           if(AccessHelperObject.IsValid(user))
            {
                //do something
                return truel
            }

           return false;
     }

     private IAccessHelper _accessHelper;
     public IAccessHelper AccessHelperObject 
     {
       get { return _accessHelper ?? ServiceLocator.Current.GetInstance(); }
       set { _accessHelper = value; }
     }

}

So that now when you write your unit test you dont have to care about service locator you can just stub out your IAccessHelper by just passing in a mock or stub to it.

  [Test]
  public void Account_DoSomething()
  {
      var account = new AccoutController();
      var stubAccess = MockRepository.GenerateStub();
      stubAccess.Expect(x => x.IsVaid("user")).Return(true);
      account.AccessHelperObject = stubAccess;
      var wasCalled = account.DoSomething();

     Assert.That(wasCalled, Is.True);
  }

This helps one to quickly break out the dependency of an object, that would otherwise take too many lines of code to test out a simple idea.

Taswar Bhatti:

View Comments (1)

Related Post