Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Injecting Fakes/Stubs into StructureMap when using Profiles

In a previous post I talked about how you can setup StructureMap to create and use profiles.  In this post I would to show you how you can swap out an instance of an object which is part of the profile with a fake/stub for testing.

I ran into this scenario the other day while creating a test and I needed to inject a mock into the ObjectFactory.  If you are not using profiles this is pretty straight forward, you simple do the following

ObjectFactory.Inject( someobjecttoinjectgoeshere);

However, when you are working with profiles this does not work (per Jeremy it may be a bug, but he is not 100% sure and I spent 30-60 minutes walking through SM code and could not determine whether it was or not).  In order to replace a instance which was going to be built based on the current profile you need to do the following.

 
ObjectFactory.Configure( x => {
			x.CreateProfile( “ProfileName”, 
				z => { z.For<ITypeeToUse>().Use( mockObjectToInject ); } 
			} );

If you use the code example above you should be able to tell the ObjectFactory to use your mock rather than an actual instance.  But be careful, this mock will remain until the profile is destroyed or you reset the object factory.

Hope this helps.

Till next time,


Posted 01-09-2009 7:52 PM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

DotNetKicks.com wrote Injecting Fakes/Stubs into StructureMap when using ProfilesA
on 01-09-2009 8:53 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Paco wrote re: Injecting Fakes/Stubs into StructureMap when using Profiles
on 01-10-2009 2:28 PM

Why don't you use the automocking container?

Derik Whittaker wrote re: Injecting Fakes/Stubs into StructureMap when using Profiles
on 01-10-2009 2:32 PM

@Paco,

Dunno, guess i did not think about using it.  Still not 100% sure it would have worked with the profiles, but worth a shot i guess.

Dew Drop - January 10, 2009 | Alvin Ashcraft's Morning Dew wrote Dew Drop - January 10, 2009 | Alvin Ashcraft's Morning Dew
on 01-10-2009 4:27 PM

Pingback from  Dew Drop - January 10, 2009 | Alvin Ashcraft's Morning Dew

Brian wrote re: Injecting Fakes/Stubs into StructureMap when using Profiles
on 01-14-2009 10:03 PM

Umm...maybe stating the obvious, but couldn't you just write a unit test to recreate the condition and go from there to figure out whether it is a bug or not???

Silverlight Travel » Injecting Fakes/Stubs into StructureMap when using Profiles wrote Silverlight Travel &raquo; Injecting Fakes/Stubs into StructureMap when using Profiles
on 01-19-2009 12:40 AM

Pingback from  Silverlight Travel &raquo; Injecting Fakes/Stubs into StructureMap when using Profiles

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)