05 December 2010

Mocking BizTalk XLANGMessage Objects

I have a method that checks for a particular node in an XLANGPart message:

bool IsValidReport(XLANGMessage report){
   XLANGMessage message = report as XLANGMessage;

   if (message != null)
   {
       string status = message[0].GetXPathValue("//*[local-name()='ReportStatus']");

       if (status.Equals("COMPLETE"))
       {
           return true;
       }
   }

   return false;
}


This method is called directly from an orchestration. Note that it relies on reading the value of a node called "ReportStatus" inside the first XLANGPart object.
To unit test this method, I mocked the XLANGMessage and XLANGPart objects:


using Microsoft.XLANGs.BaseTypes;
using Moq;
...

Mock<XLANGMessage> xlangMsgMock = new Mock<XLANGMessage>();
Mock<XLANGPart> xlangPartMock = new Mock<XLANGPart>();

xlangPartMock.Setup(p => p.GetXPathValue(It.IsAny<string>())).Returns("COMPLETE");
xlangMsgMock.SetupGet<XLANGPart>(m => m[0]).Returns(xlangPartMock.Object);

bool isValid = ReportManager.IsValidReport(xlangMsgMock.Object);

xlangPartMock.VerifyAll();
xlangMsgMock.VerifyAll();


Here I've mocked the GetXPathValue method to return "COMPLETE" which will make the IsValidReport method return true. I've then attached this XLANGPart mock to the XLANGMessage mock and called the method to test.
Additional unit tests can be mocked to return other status values to simulate other conditions.

No comments: