Search code examples
c#nunitsystem.text.json

System.Text.Json / NUnit - Unit testing custom Json converter / Utf8JsonReader


I have a series of test methods to verify whether my custom Json converter (System.Text.Json) throws expected exceptions. One of such method:

[Test]
public void ReadJsonTokenNullThrows()
{
    // Arrange

    // Act & Assert
    var exception = Assert.Throws<JsonException>(() =>
    {
         var utf8JsonReader = new Utf8JsonReader(Encoding.UTF8.GetBytes("null"), false, new JsonReaderState(new JsonReaderOptions()));
         utf8JsonReader.Read();

         sut.Read(ref utf8JsonReader, typeof(DateTime), serializerOptions);
    });

    Assert.AreEqual("JSON value was a literal null", exception.Message);
}

I'd like to have only the line sut.Read(...); inside the Assert.Throws.. code block, however when I move the Utf8JsonReader initialization and utf8JsonReader.Read() outside of it, I get the compiler error:

Cannot use ref local 'utf8JsonReader' inside an anonymous method, lambda expression, or query expression.

Is there a way around to do this? Preferably without the try/catch


Solution

  • Utf8JsonReader is a ref-struct and hence it cannot be passed to the lambda method. See:

    Assert.Throws<FormatException>(() =>
            {
                var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json), options);
                while (reader.TokenType == JsonTokenType.None)
                {
                    if (!reader.Read())
                    {
                        break;
                    }
                }
    
                _target.Read(ref reader, typeof(DateOnly), null);
            });
    

    where _target is a custom DateOnlyConverter