Blog Moved to http://podwysocki.codebetter.com/

Blog Moved to http://podwysocki.codebetter.com/
posts - 277, comments - 131, trackbacks - 27

My Links

News

Disclaimer
The views expressed on this weblog are mine and do not necessarily reflect the views of my employer.

All postings are provided "AS IS" with no warranties, and confer no rights.

 Subscribe in a reader


I'm test-driven!
Locations of visitors to this page

Archives

Post Categories

Image Galleries

ALT.NET

Blogs

Mailing Lists

OSS Projects

Other

  • Blog Flux Directory

User Groups

Rhino Mocks and cutom BizTalk code revisited

In a previous post, I talked about abstracting the BizTalk XLANGMessage away from the code through an IOrchestrationMessage interface.  After some deliberation and inspiration from Phil Haack, I decided to take another look at using Rhino Mocks to help my cause.

In this case, I'd like to use Extension Methods, but I'd rather not mix and match Visual Studio 2005 with 2008 just yet.  But you could imagine it would look something like this when done:

public static XLANGMessage CreateBizTalkMessage(this MockRepository repository)
{
    XLANGMessage message = repository.DynamicMock<XLANGMessage>();
    XLANGPart part = repository.DynamicMock<XLANGPart>();
   
    SetupResult.For(message[0]).Return(part);
    SetupResult.For(message.GetPropertyValue(typeof(FILE.ReceivedFileName))).Return(@"C:\index.xml");
    FileStream stream = new FileStream(@"C:\index.xml", FileMode.Open, FileAccess.Read);
    SetupResult.For(part.RetrieveAs(typeof(Stream))).Return(stream);
   
    repository.ReplayAll();

    return message;
}

Pretty simple code and easy to use.  Rhino Mocks handles this perfectly and I'm able to use them in my test methods like so.

[Test]
public void OrchestrationHelper_ProcessMessage_Success()
{
     MockRepository repository = new MockRepository();
     XLANGMessage btsMessage = repository.CreateBizTalkMessage();

     ...
}

So, at the end of the day, it makes my life much easier to test parts of my custom code that requires me to hand it the BizTalk XLANG message.  No fake interfaces required!

kick it on DotNetKicks.com

Print | posted on Thursday, November 29, 2007 12:46 AM | Filed Under [ Microsoft BizTalk .NET C# ]

Feedback

Gravatar

# re: Rhino Mocks and cutom BizTalk code revisited

That's an awesome use of Extension Meathods.

I typically inherit from the MockRepository and add instance methods like CreateBizTalkMessage to the super class.

I think I still like that for building complex object hierarchies.

For example mocking things like HTTPContext. The super class would have at least three member properties. MockContext, MockRequest and MockResponse. When the MockConext is created it sets the result for the Request and Response to the MockRequest and MockResponse. Now when I'm testing I can write something like this.

ClassUnderTest cut = new ClassUnderTest();
MyMockRepository mocks = new MyMockRepository();
Expect.Call( mocks.MockRequest["qsParam1"] ).Returns("someValue");
Expect.Call( mocks.MockResponse.Redirect("http://www.google.com") );
mocks.ReplayAll();
cut.Execute( mocks.MockContext );

I see that being useful in your scenario also.
For instance you might want to have the part.RetrieveAs thow an exception instead of returning a stream. You could set that up from a test a lot easier if you have access to the XLANGPart instance apart from the XLANGMessage instance.

I suppose you could also overload the CreateBizTalkMessage function to inject the XLANGPart and have multiple factory/Extension Methods to create the XLANGPart instance for multiple scenarios.
12/7/2007 1:05 AM | Aaron Carlson
Gravatar

# re: Rhino Mocks and cutom BizTalk code revisited

You're right on the money in terms of making factories and such of creating XLANGParts. Indeed, I want to create a variation of these XLANGPart classes in which some have valid and some have invalid data. So, I could have the overall XLANGMessage creation method and a bunch of XLANGPart creators that test each permutation of what could be in the document and how I could react to it. That's the beauty of mocking and dependency injection.

I like that code example and find the pattern pretty useful.
12/7/2007 11:03 AM | Matthew Podwysocki
Gravatar

# re: Rhino Mocks and cutom BizTalk code revisited

This code does not work for me
cannot create mock for indexer message[0]
can you let me know which version of rhino mock you are using ?

Aron
10/20/2008 6:21 AM | Aron
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: