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
Using Moq with StructureMap causing a 220 error

While writing some code today I thought I would give Moq a shot in place of RhinoMocks.  One thing I was very happy about was that I found it very easy to pickup, which is a plus. 

However, I did want to mention one small gotcha I ran into.  I was trying to inject a mock into StructureMap via the ObjectFactory.InjectStub() like i have done numerous times in the past.  However this time i received the following SM error.

StructureMap.StructureMapException: StructureMap Exception Code:  220
Cannot "Stub" type ......Repositories.ITrackingRepository with an object of type Moq.Mock`1

The good news is this error is real easy to overcome.  Below you will find the 'breaking code' along with the 'working code'

Breaking Code

public void TrackEpisodeDetailsView_ExpectCallToRepository()
{
    var mockTrackingRepository = new Mock();

    mockTrackingRepository.Expect( x => x.LogEpisodeDetailsView( It.IsAny(), It.IsAny() ) ).Returns( new TrackingEpisodeView() );

    ObjectFactory.InjectStub( typeof(ITrackingRepository), mockTrackingRepository );

    var tracking = new Tracking();

    tracking.TrackEpisodeDetailsView( 1, "" );

}

Working Code

public void TrackEpisodeDetailsView_ExpectCallToRepository()
{
    var mockTrackingRepository = new Mock();

    mockTrackingRepository.Expect( x => x.LogEpisodeDetailsView( It.IsAny(), It.IsAny() ) ).Returns( new TrackingEpisodeView() );

    ObjectFactory.InjectStub( typeof(ITrackingRepository), mockTrackingRepository.Object );

    var tracking = new Tracking();

    tracking.TrackEpisodeDetailsView( 1, "" );

}

You will notice the one slight change.  When passing the mock into the InjectStub method you need to use the mock.Object property to actually return the mock instance, not the Moq wrapper.

Till next time,


Posted 05-11-2008 11:07 AM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

Rob G wrote re: Using Moq with StructureMap causing a 220 error
on 07-16-2008 5:23 AM

Mate! You've just saved my bacon! Not because of the ".Object" part, I got that - this post was infinitely more useful to me because of the InjectStub part - it was under my nose all the time...doh!

Been racking my brains for hours...much oblidged!

Rob

Bill Sorensen wrote re: Using Moq with StructureMap causing a 220 error
on 10-06-2008 8:33 PM

I ran into exactly the same thing today with Unity. Fortunately it's easy to fix!

I'm really liking Moq a lot.

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)