Mocking with Parameters Content

These examples apply if your messaging activities use parameters content.

Service Contract Name

Most of the time all of the activities in your service will use the same service contract name so you can declare a field with the contract name in the test class
private readonly XName serviceContractName = XName.Get("{http://tempuri.org/}IService");

Mocking Parameters Content with One Receive/Send

Given
  • A WorkflowService with a GetData Operation which accepts two parameters value1 and value2 and returns a composite response with string parameters data1 and data2
When
  • The service is invoked using mocked Send/Receive with parameters value1=123 and value2=456
Then
  • The service can be invoked without sending a WCF message
  • The mock implementations are used
  • The response parameters are data1="123" and data2="456"
[TestMethod]
[DeploymentItem(@"MockingMessagingActivities.Tests\Service1Parameters.xamlx", "MockingMessagingActivities")]
public void CanMockReceiveAndSendReplyWithParameters()
{
    // Arrange
    const int Value1 = 123;
    const int Value2 = 456;
    var xamlInjector = new XamlInjector(@"MockingMessagingActivities\Service1Parameters.xamlx");

    // Setup the XamlInjector to replace the receive / send activities
    xamlInjector.ReplaceAll(typeof(Receive), typeof(ReceiveStub));
    xamlInjector.ReplaceAll(typeof(SendReply), typeof(SendReplyStub));

    // Access the workflow service Body activity for testing with WorkflowInvoker
    var host = WorkflowInvokerTest.Create(xamlInjector.GetWorkflowService().Body);

    // Setup the extension
    dynamic arguments = new WorkflowArguments();
    arguments.value1 = Value1;
    arguments.value2 = Value2;

    var stubExtension = new MessagingStubExtension();
    stubExtension.EnqueueReceive(this.serviceContractName, "GetData", arguments);
    host.Extensions.Add(stubExtension);

    try
    {
        host.TestActivity();

        // The first reply message parameter data1 should be "123"
        Assert.AreEqual(Value1.ToString(), stubExtension.SentMessages[0].Parameter("data1"));

        // The first reply message parameter data2 should be "456"
        Assert.AreEqual(Value2.ToString(), stubExtension.SentMessages[0].Parameter("data2"));
    }
    finally
    {
        host.Tracking.Trace();
    }
}

Mocking Two Receive and SendReply activities with parameters content

In this case we have two Receive and SendReply pairs so we must setup the stub to provide the parameters content in the order they will be invoked.
Given
  • A WorkflowService with operation GetData which accepts two parameters value1 and value2
  • A second method GetData2 which continues the workflow, adds 1 to value1 and 2 to value2 to the data and returns strings data1 and data2
When
  • The service is invoked using mocked Send/Receive with value1=5 and value2=4
Then
  • The first reply is data1="5" and data2="4"
When
  • The service is invoked using mocked Send/Receive with value1=6 and value2=3
Then
  • The second reply is data1="7" and data2="5"
[TestMethod]
[DeploymentItem(@"MockingMessagingActivities.Tests\Service2Parameters.xamlx", "MockingMessagingActivities")]
public void CanMockTwoReceiveAndSendReplyWithParameters()
{
    // Arrange
    var xamlInjector = new XamlInjector(@"MockingMessagingActivities\Service2Parameters.xamlx");

    // Setup the XamlInjector to replace the receive / send activities
    xamlInjector.ReplaceAll(typeof(Receive), typeof(ReceiveStub));
    xamlInjector.ReplaceAll(typeof(SendReply), typeof(SendReplyStub));

    // Access the workflow service Body activity for testing with WorkflowInvoker
    var host = WorkflowInvokerTest.Create(xamlInjector.GetWorkflowService().Body);

    // Setup the extension
    var stubExtension = new MessagingStubExtension();

    // Setup the first message
    dynamic parameterValues1 = new WorkflowArguments();
    parameterValues1.value1 = 5;
    parameterValues1.value2 = 4;
    stubExtension.EnqueueReceive(this.serviceContractName, "GetData", parameterValues1);

    // Setup the second message
    dynamic parameterValues2 = new WorkflowArguments();
    parameterValues2.value1 = 6;
    parameterValues2.value2 = 3;
    stubExtension.EnqueueReceive(this.serviceContractName, "GetData2", parameterValues2);

    host.Extensions.Add(stubExtension);

    try
    {
        host.TestActivity();

        // Assert
        // The first reply parameter "data1" is "5"
        Assert.AreEqual("5", stubExtension.SentMessages[0].Parameter("data1"));

        // The first reply parameter "data1" is "4"
        Assert.AreEqual("4", stubExtension.SentMessages[0].Parameter("data2"));

        // The second reply parameter "data1" is "7"
        Assert.AreEqual("7", stubExtension.SentMessages[1].Parameter("data1"));

        // The second reply parameter "data1" is "5"
        Assert.AreEqual("5", stubExtension.SentMessages[1].Parameter("data2"));
    }
    finally
    {
        host.Tracking.Trace();
    }
}

Last edited Nov 12, 2011 at 8:39 PM by ronjacobs, version 8

Comments

No comments yet.