Search code examples
c#arraysjsonenumstdd

Declaring JSON Enum Array in C# for datamember and TDD deserialization


I have this enum created in a file, and usage for an enum array in datamember. For test-driven development, I have a difficulty to setup for the test. Below are the example

In a *.cs file i declared

public enum StatusType
{

   [EnumMember(Value = "Ok")]
   Ok =0,

   [EnumMember(Value = "Warning")]
   Warning,

   // ...
}

Part of the CRUD is update, and in this model part, where i have set it as example:

public partial class Patch
{
   // ...

   [DataMember(Name = "status:enum")]
   public StatusType[] StatusResult { get; set; }

}

*Notice the array declared.

For TDD, how do I test for this? Having two enum defined at the same time?

Example in JSON string:

"status:enum": ["ok", "warning"]``

The most important part is the TDD where the test able to compare the result in the enum array.


Solution

  • It's not test-driven development (TDD) if you don't drive the design and implementation with tests. Writing the tests after the types isn't TDD, but it's still automated testing.

    It's not clear what you mean by

    For TDD, how do I test for this?

    but from the second question, I gather that you're asking about how to set the values according to the JSON example. You can do that in a test like this:

    [Fact]
    public void TestExample()
    {
        var patch = new Patch
        {
            StatusResult = new[] { StatusType.Ok, StatusType.Warning }
        };
    
        // Do something with `patch`, e.g.:
        Assert.Contains(StatusType.Ok, patch.StatusResult);
    }
    

    This example uses xUnit.net.