Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking without injection

(C#, WCF Service, Rhino Mocks, MbUNit)

I have been writing tests for code already in place (yes I know its the wrong way around but that's how its worked out on my current contract). I've done quite a bit of re-factoring to support mocking - injecting dependencies, adding additional interfaces etc - all of which have improved the design. Generally my testing experience has been going well (exposing fragility and improving decoupling). For any object I have been creating the dependant mocks and this sits well with me and makes sense.

The app essentially has 4 physical layers. The database, a repository layer for data access, a WCF service that is connected to the repository via a management (or business logic) layer, so top down it looks like this;

WCF Managers Repository Database

Testing the managers and repository layer has been simple enough, mocking the dependencies with Rhino Mocks and injecting them into the layer under test as such.

My problem is in testing the top WCF layer. As my service doesn't have the constructor to allow me inject the dependencies, I'm not sure how I go about mocking a dependency when testing the public methods (ServiceContracts) on the service.

I hope that's made sense and any help greatly appreciated. I am aware of TypeMockIsolator etc, but really don't want to go down that route both for budget and other reasons I won't go into here. Besides I'm sure there are plenty of clever 'Stackers' who have the info I need.

Thanks in advance.

like image 971
Simon Rigby Avatar asked Nov 06 '22 09:11

Simon Rigby


1 Answers

Is there a specific reason you cant have a constructor on your service?

If not, you can have overloaded constructors with a default constructor wiring up the defaults and one parametrized constructor with your dependencies. You can now test the parametrized ctor and rely on the default ctor for creating the instance in production.

public MyService() : this(new DefaultDep1(), new DefaultDep2())
{
}

public MyService(IDep1 d1, IDep2 d2)
{
}

A better solution if you use dependency injection would be to use the WCF IInstanceProvider interface to create your service instance and provide the needed dependencies via that injection point. An example using Structure Map can be found here.

like image 54
PHeiberg Avatar answered Nov 14 '22 21:11

PHeiberg