Friday, October 26, 2012

Testing Workflows

Up to this point the application is not very interesting. It is only working in a console and does not receive any input arguments. Most applications that do meaningful work will have to deal with input and output arguments. Additionally, the application is not easily tested in its current form.

From last post Dynamic Workflows with XAML we will continue our project we will modify the SayHello activity to use arguments and prepare it to be useful in other non-console application environments by returning a greeting rather than writing directly to the Console with the WriteLine activity. We will be doing this using the "write the test first" approach. First we will create a test that fails and then add the necessary code to make it pass.
The resulting application will be the equivalent of the following code. 

C#
private static string SayHello(string name)
{
  return "Hello " + name + " from Workflow 4";
}

Visual Basic
Private Shared Function SayHello(ByVal name As String) As String
  Return "Hello " & name & " from Workflow 4"
End Function
  1. Open HelloWorkFlow project from last post Dynamic Workflows with XAML . or download it from this links:EzzFile ,

    Uploaded ,
    Depositfiles
  2. To begin with, create a unit test for the workflow to ensure that it behaves as it should. In Solution Explorer, right-click the HelloWorkFlow solution and point to Add and click New Project. In the Add New Project dialog box, select the Test Project under the Test project type in either the Visual C# or Visual Basic project Templates list and enter HelloWorkflow.Tests as the project Name. Leave the location by default
    Adding a new Test Project to the solution (C#)
    Adding a new Test Project to the solution (C#)
      Adding a new Test Project to the solution (Visual Basic)
      Adding a new Test Project to the solution (Visual Basic)

      1. Right-click the HelloWorkflow.Tests project and click Add Reference. Using the Projects tab, add a project reference to the HelloWorkflow project. Repeat these steps, using the .NET tab instead to add a reference to the System.Activities library.
      2. Right-click UnitTest1.cs (C#) or UnitTest1.vb (VB), click Rename and change its name to SayHelloFixture.cs (C#) or SayHelloFixture.vb (VB). When prompted to rename the UnitTest1 class select Yes.
      3. Add the following namespace directive to the SayHelloFixture.cs (C#) file or SayHelloFixture.vb (VB):
        C#
        using System.Activities;

        Visual Basic
        Imports System.Activities
      4. The SayHello activity doesn’t accept any arguments yet, but we will write the code to invoke it as though it does. This will allow us to think about what the interface to our activity feels like when consuming it. Don’t worry if Visual Studio warns us about the UserName property not being defined yet. Replace the TestMethod1 with the following code:
        C#
        [TestMethod]
        public void ShouldReturnGreetingWithName()
        {
          var output = WorkflowInvoker.Invoke(
            new SayHello()
            {
              UserName = "Test"
            });
          Assert.AreEqual("Hello Test from Workflow 4", output["Greeting"]);
        }

        Visual Basic
        <TestMethod()> Public Sub ShouldReturnGreetingWithName()
          Dim output = WorkflowInvoker.Invoke( _
            New SayHello() With {.UserName = "Test"})
          Assert.AreEqual("Hello Test from Workflow 4", output("Greeting"))
        End Sub
          Notes: How do I pass arguments to an activity?
        We can create the activity and initialize the arguments (which are public properties) using object initialization or we can pass a Dictionary<string, object> (C#) or Dictionary(Of String, Object) (VB) of input parameters that map to the names of the input arguments of the activity.
          How do I get data from output?
        The output variable is an IDictionary<string, object> (C#) or IDictionary(Of String, Object) (VB) that contains a map of output variables using the name of the variable as the key.
      5. CTRL+SHIFT+B to build the application it should fail with a compile error
        UserName has not been defined yet (C#)
        UserName has not been defined yet (C#)
        UserName has not been defined yet (VB)
        UserName has not been defined yet (VB)
          1. Open SayHello.xaml in the designer.
          2. Open the arguments pane by clicking on Arguments
            Click on Arguments to open the arguments pane
            Click on Arguments to open the arguments pane
            1. Add the UserName and Greeting arguments as shown in the following screenshot:
              Declare the arguments to the activity
              Declare the arguments to the activity
                 Note: Arguments
                In Windows Workflow Foundation (WF), arguments represent the flow of data into and out of an activity. An activity has a set of arguments and they make up the signature of the activity. Each argument has a specified direction: input, output, or input/output.
              1. Press CTRL+SHIFT+B to build the solution, it should now build without errors.
              2. Open the SayHelloTestFixture.cs (C#) or SayHelloTestFixture.vb (VB) file.
              3. Press CTRL+R, T to run the unit tests in the current context. The test will run but it will fail, because the activity is not returning anything in the Greeting out argument.
                ShouldReturnGreetingwithName test
                ShouldReturnGreetingwithName test
                1. Writing text to the console with WriteLine isn’t going to make the test pass so delete the WriteLine activity by right clicking on it and selecting Delete(X).
                2. You need to set the value of the Greeting out argument. To do this, drag an Assign activity from the Toolbox and drop it onto the designer surface.
                  Drag an Assign activity to the design surface
                  Drag an Assign activity to the design surface
                  1. Set the To property to Greeting.
                    Type Greeting in the left box
                    Type Greeting in the left box
                    1. We could type the greeting expression into the design surface but since our expression is longer, use the properties window. Click the button to the right of the Value property to open the expression editor.
                      Click the button to the right of the Value property
                      Click the button to the right of the Value property
                      1. In the expression editor, you can enter longer expressions. Set the expression to "Hello " & UserName & " from Workflow 4"
                        The Assign activity that sets the Greeting argument
                        The Assign activity that sets the Greeting argument
                        1. Press CTRL+SHIFT+B to build the solution, it should compile without errors.
                        2. Open the Test View window. To do this, on the Test menu point to Windows and click Test View .
                        3. In the Test View, select the ShouldReturnGreetingWithName test and click the Run Selection button.
                          Selecting and running the ShouldReturnGreetingWithName test
                          Selecting and running the ShouldReturnGreetingWithName test
                          1. Verify that the test run passes.
                            The test passed
                            The test passed

                            1 comment:

                            Automatic Traffic Exchange

                            YallaTech Facebook page