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