I'm possibly doing something very stupid, but I'm wondering if there's any difference in how things get run in a test and when the program itself gets run? I have this test:
[<TestMethod>]
member _.UnixEpoch_CorrectlyDeserializesDateTime () =
// Arrange
let expected = DateTime(2024, 9, 27, 0, 0, 0, DateTimeKind.Utc)
let timestamp = 1727395200000L
let json = $"""{{"Timestamp":{timestamp}}}"""
// Act
let actual = Json.deserializeF<UnixEpochRecord> json
// Assert
Assert.AreEqual<DateTime>(expected, actual.Timestamp)
Where these bits are relevant:
module Json =
let options =
JsonFSharpOptions()
.WithUnionUnwrapFieldlessTags()
.WithSkippableOptionFields(SkippableOptionFields.Always, deserializeNullAsNone = true)
.ToJsonSerializerOptions()
let deserializeF<'a> (json: string) =
JsonSerializer.Deserialize<'a>(json, options)
module Converters =
type UnixEpoch () =
inherit JsonConverter<DateTime> () with
override _.Read (reader, typeToConvert, options) =
DateTimeOffset.FromUnixTimeMilliseconds(reader.GetInt64()).DateTime
override _.Write (writer, value, options) =
DateTimeOffset(value).ToUnixTimeMilliseconds() |> writer.WriteNumberValue
type UnixEpochRecord = {
[<JsonConverter(typeof<Converters.UnixEpoch>)>] Timestamp: DateTime
}
It is supposed to convert some json with a long from a unix timestamp to a DateTime. When I use this when running my program seems to works fine, but when I run the test it fails:
Test method Discordfs.Types.ConvertersTests.UnixEpoch_CorrectlyDeserializesDateTime threw exception:
System.Text.Json.JsonException: The JSON value could not be converted to System.DateTime. Path: $ | LineNumber: 0 | BytePositionInLine: 13. ---> System.InvalidOperationException: Cannot get the value of a token type 'Number' as a string.
It seems to fail with any use of json serialization, which I'm doing with FSharp.SystemTextJson, since all my other tests that involve it aren't working either. Any ideas what I may have done wrong here?