Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



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 -----]



Comments

Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew said:

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

# October 10, 2008 9:48 AM

Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew said:

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

# October 12, 2008 6:05 PM

DotNetKicks.com said:

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

# November 7, 2008 9:45 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Red-Gate!