2

I'm writing unit tests in F# using FsUnit and NUnit, with the NUnit test adapter for VS2015 Ultimate CTP. I've come across an odd issue with a module member being null, where I wouldn't expect it to be.

Is this an issue with the the code, or the way the tests are executed?

I've tried changing the signature of Foo.SpecificFooStrategy.create to unit -> FooStrategy (let create = fun () -> ...) and invoking as Foo.SpecificFooStrategy.create (), but that hasn't helped.

Code

namespace Foo

// FooStrategy.fs
module FooStrategy =
    type FooStrategy = 
        | FooStrategy of A * B
        with
        member x.A = 
            match x with
            | FooStrategy(a, _) -> a

        member x.B = 
            match x with
            | FooStrategy(_, b) -> b

    let create a b = FooStrategy(a, b)

// SpecificFooStrategy.fs
module SpecificFooStrategy =
    let private a = // ...
    let private b = // ...

    let create =
        FooStrategy.create a b

Test

namespace Foo.Tests

[<TestFixture>]
module SpecificFooStrategyTests =
    [<Test>]
    let ``foo is something`` ()=
        let strategy = Foo.SpecificFooStrategy.create

        strategy // strategy is null here
        |> An.operationWith strategy
        |> should equal somethingOrOther
Andy Hunt
  • 1,053
  • 1
  • 11
  • 26

1 Answers1

1

In the code, create is not a function but a value.

It can be fixed by defining it as a function:

let create() = …

and calling it with parens:

let strategy = Foo.SpecificFooStrategy.create()
Be Brave Be Like Ukraine
  • 7,596
  • 3
  • 42
  • 66
  • 1
    That solved the problem perfectly, thanks. Out of curiosity, what's the difference between defining `create` as `fun () -> ...` and `create() = ...`? They both have the same type signature. – Andy Hunt May 03 '15 at 14:15