Andy Hunt
Andy Hunt

Reputation: 1073

F# module member uninitialized

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

Upvotes: 2

Views: 95

Answers (1)

Be Brave Be Like Ukraine
Be Brave Be Like Ukraine

Reputation: 7735

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()

Upvotes: 1

Related Questions