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
Partial Mocks with the StructureMap AutoMocker

If you have not checked out the new AutoMocking Container that is part of StructureMap 2.5 I would highly suggest you do, it rocks.

I thought I would show how simple it is to create a PartialMock on the Class under Test (CUT) when using the RhinoAutoMocker.

Our Class we want to mock

public class VisitReassignmentRule : PersistingRule
{

    public VisitReassignmentRule( ISynchronizationRepository synchronizationRepository, 
	IValidationRepository validationRepository, 
        IValidationLogger validationLogger ) : base( validationRepository, validationLogger )
    {
        SynchronizationRepository = synchronizationRepository;
    }

    public override InvocationActions ProcessRule( DataSet ds )
    {

	// logic goes here

        return null;
    }

    public override bool DataIsInviolationOfRule( DataSet dataSet )
    {
        return false;
    }

    private ISynchronizationRepository SynchronizationRepository { get; set; }
}

Mocking Code

[Test]
public void ProcessRule_ViolatesRule()
{
    	var rule = new RhinoAutoMocker();

	// This line here tell the mocking container to make it a partial ( I know, the method name tells u that to)
    	rule.PartialMockTheClassUnderTest();
    	rule.ClassUnderTest.Expect( x => x.DataIsInviolationOfRule( null ) ).IgnoreArguments().Return( false ).Repeat.Any();

	// actually call the method
    	rule.ClassUnderTest.ProcessRule( dataset );
}

As you can see, telling the AutoMocking container that you want the Class under Test to be a partial is very easy.  The one thing you want to make sure you do is not use the .Get<> syntax and just use the .ClassUnderTest syntax to set your expectation.

Till next time,

[----- Remember to check out DimeCasts.Net -----]


Posted 10-10-2008 7:54 AM by Derik Whittaker

[Advertisement]

Comments

Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew
on 10-10-2008 9:48 AM

Pingback from  Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew

Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew
on 10-12-2008 6:05 PM

Pingback from  Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew

DotNetKicks.com wrote Partial Mocks with the StructureMap AutoMocker
on 11-07-2008 9:45 AM

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

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)