0
votes

I am working with distributed WF application, where I've created several own Activities. Some of them have mandatory InArguments that I would like to test. What I am trying to achieve is to execute the activity without any InArgument and assert expected ArgumentException's exception message with dynamically generated exception message I expect to be thrown.

As an example, let's have following activity

public class MyActivity : CodeActivity<bool>
{
    public MyActivity()
    {
        // some logic
    }

    [RequiredArgument]
    public InArgument<string> ArgA { get; set; }

    [RequiredArgument]
    public InArgument<string> ArgB { get; set; }

    [RequiredArgument]
    public InArgument<string> ArgC { get; set; }
}

I am using NUnit in combination with Microsoft.Activities.UnitTesting

[TestFixture]
public class MyActivityTests
{
    [Test]
    public void ValidateContainsMandatoryFields()
    {
        var activity = new MyActivity();
        var mandatoryFields = new ReadOnlyCollection<string>(new [] { "ArgA", "ArgB", "ArgC" });
        var expectedExceptionMsg = GenerateMsg(activity.DisplayName, mandatoryFields);
        var sut = new WorkflowInvokerTest(activity);
        var ex = Assert.Throws<ArgumentException>(delegate { sut.TestActivity(); });
        Assert.AreEqual(expectedExceptionMsg, ex.Message);
    }

    private string GenerateMsg(string displayName, IReadOnlyCollection<string> mandatoryFieldsNames)
    {
        var sb = new StringBuilder(@"The values provided for the root activity's arguments did not satisfy the root activity's requirements:");

        foreach (var fieldName in mandatoryFieldsNames)
        {
            sb.Append(string.Format("\r\n'0': Value for a required activity argument '{1}' was not supplied.", displayName, fieldName));
        }

        sb.Append("\r\nParameter name: rootArgumentvalues");
        return sb.ToString()
    }
}

The problem I am facing is, that I do expect the InArguments to be ordered for example Alphabetically (argA, argB, argC), but for some reason, fields are ordered what seems to me as randomly (but always in the same order). In my case, for example, argB, argA, argC.

Is there any order that I am missing?

1

1 Answers

0
votes

I think you are writing a test to test the format of the error message rather than which fields are in fact mandatory or not. The required field list does not necessarily get stored as a list internally, so there is no guarantee as to how they will be read out of the collection.

I would write 4 test cases where you supply values to all the parameters, then one each where you miss out each argument, confirming that the exception is thrown and that those error messages contain the name of the expected parameter.