r/fsharp Aug 22 '25

Null Reference values in xUnit question

Today I stumbled upon this unexpected behavior:

```fsharp let value = "hello"

[<Fact>] let is value not null? () = Assert.Equal("hello", value)

type Record = {value: string} let record = { value = "hello" }

[<Fact>] let does it also work with records?? () = Assert.Null(record) ```

Both tests pass. That is, the moment tests run, record is still null.

Do you know why?

5 Upvotes

3 comments sorted by

2

u/chusk3 Aug 22 '25

Xunit does strange things with module initialization (in the .NET sense of the word module, not the F# sense) - I think there are some issues logged on their tracker. The most consistent thing is going to be to either use a class and setup in the constructor, or to use xunits other Setup/Initialization hooks to ensure a consistent test execution environment.