Ivan Afonichkin
Ivan Afonichkin

Reputation: 103

F# NUnit test sets value as null

I have 2 files: Asm.fs, AsmTest.fs

Asm.fs

namespace Assembler

[<Measure>] type ln
[<Measure>] type col
[<Measure>] type port

type Addr = int<ln> * int<col> * int<port>

module Asm = 
    let emptyAddr : Addr = (-1<ln>, -1<col>, -1<port>)

AsmTest.fs

module Assembler.Tests

[<Test>]
let someTest() = 
    let x = Asm.emptyAddr

When I debug someTest() code, I get that x = null, what am I doing wrong?

P.S. files in the different projects visual studio projects. Asm.fs in the Assembler project and AsmTest.fs in the AssemblerTest project.

I found an interesting behavior. My problem will be resolved, if I add some file (even empty) to the Assembler project. Can anyone explain this behavior?

Upvotes: 3

Views: 137

Answers (1)

CaringDev
CaringDev

Reputation: 8551

The debugger sometimes has issues showing the correct values. For me however, having exactly the same Asm.fs and AsmTest.fs like this:

module Assembler.Tests

open NUnit.Framework

[<Test>]
let someTest() = 
    let x = Asm.emptyAddr
    Assert.IsNotNull x

the test passes and if I set a breakpoint at the assertion, x is correctly shown as {(-1, -1, -1)}

As the code that you show does not compile as it is (Block following this let is unfinished. in someTest), could you try my test above and/or show your complete test method?

Using your code I can reproduce the behaviour. If I set my projects to console applications, my test will fail as well. So, it seems that for console projects, not having any other file or an [<EntryPoint>] surprisingly skips the initialization of module values. For me, the compiler at least issues a warning Main module of program is empty where I use x in the test. The solution to the problem is therefore:

  • make sure you treat warnings as errors
  • have an [<EntryPoint>] for console applications
  • use library projects for libraries

Upvotes: 2

Related Questions