Mocking DependencyResolver


This may be known to some of you, but I’d never had to do this before, so I thought I’d share.

I had some MVC 4 code that explicitly calls the System DependencyResolver (the application uses Unity for dependency injection) to retrieve a service:

Mocking (in this case) the ISomeOtherService is obviously a trivial matter and, fortunately, it turns out it is quite easy get the DependencyResolver to return the mock as well.  Using NUnit and NSubstitute, it can achieved with the minimum hassle as below

Feel free to ask if anything is unclear.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s