Injecting Typemock Stubs and Mocks using the StructureMap container in a unit test

My blog has moved. You can view this post at the following address: http://www.osherove.com/blog/2008/4/24/injecting-typemock-stubs-and-mocks-using-the-structuremap-co.html
Published Thursday, April 24, 2008 7:40 PM by RoyOsherove

Comments

Thursday, April 24, 2008 9:42 PM by Derik Whittaker

# re: Injecting Typemock Stubs and Mocks using the StructureMap container in a unit test

Hey, I have seen this some place not that long ago.... Hum where could that have been.

Thanks for the help.

Friday, April 25, 2008 10:34 AM by Dave

# re: Injecting Typemock Stubs and Mocks using the StructureMap container in a unit test

I don't get it.  

1.  If your subject class is already configured for injection, then why would it need to use the injection framework itself?  Wouldn't it just have its dependencies injected (via constructor or setter)?

2.  If you need to test a class that uses the DI framework, you're better off putting a layer of abstraction between the subject class and your DI framework anyway, in case you want to switch frameworks in the future.  And in that case, you just mock your abstraction layer instead of injecting mocks into the DI config.

Or am I missing something?

Friday, April 25, 2008 11:54 AM by ISerializable - Roy Osherove's Blog

# Pros and Cons and of using an Auto Mocking container in your tests

In regards to my post about injecting mocks and stubs using a container in your tests, Dave asks in the

Friday, April 25, 2008 12:03 PM by .Net World

# Pros and Cons and of using an Auto Mocking container in your tests

In regards to my post about injecting mocks and stubs using a container in your tests, Dave asks in the

Friday, April 25, 2008 12:22 PM by Jeremy

# re: Injecting Typemock Stubs and Mocks using the StructureMap container in a unit test

Roy,

You need to do this:

   ObjectFactory.InjectStub<IEmailer>(emailStub.MockedInstance);

or

ObjectFactory.Inject<ILogger>(logMock.MockedInstance);

You have to inject an object against the desired "PluginType."  By omitting the T in Inject<T>() StructureMap plugs it into "object."