F# 模块成员未初始化

F# module member uninitialized

我正在使用 FsUnit 和 NUnit 在 F# 中编写单元测试,并使用适用于 VS2015 Ultimate CTP 的 NUnit 测试适配器。我遇到了一个模块成员为空的奇怪问题,我不希望它是空的。

这是代码问题还是测试执行方式的问题?

我试过将 Foo.SpecificFooStrategy.create 的签名更改为 unit -> FooStrategy (let create = fun () -> ...) 并以 Foo.SpecificFooStrategy.create () 的身份调用,但这没有帮助。

代码

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

测试

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

在代码中,create不是一个函数而是一个值。

可以通过将其定义为函数来解决:

let create() = …

并用括号调用它:

let strategy = Foo.SpecificFooStrategy.create()