我们正在使用非常棒的FSUnit进行单元测试。这很好用,除了我们的测试主体坚持使用完整的F#语法(在每一行的末尾加上'in‘等)。而不是#light语法。例如:
module MyTests
open System
open NUnit.Framework
open FsUnit
open MyModule
[<TestFixture>]
type ``Given a valid file`` () =
let myFile = createSomeFile()
[<Test>] member x.
``Processing the file succeeds`` () =
let actual = processFile myFile in
actual |> should be True注意测试第一行末尾的“in”。如果没有这一点,测试将无法编译-这对于较短的测试是很好的,但对于较长的测试方法则是一种痛苦。我们已经尝试在源代码中添加一个显式的#light,但似乎没有什么不同。这是一个包含许多模块的大型项目的一部分,除了测试模块之外,所有模块都愉快地使用了轻量级语法(没有任何明确的# light )。是什么触发了测试模块中的完整语法?
发布于 2012-05-18 17:14:03
在编写类的成员时,需要使用一些不同的缩进。以下内容应该没问题:
[<TestFixture>]
type ``Given a valid file`` () =
let myFile = createSomeFile()
[<Test>]
member x.``Processing the file succeeds`` () =
let actual = processFile myFile
actual |> should be True 第一个问题是成员的名称应该比.缩进得更远,第二个问题是成员的主体应该比member关键字缩进得更远-在您的版本中,关键字写在[<Test>]之后,所以如果您进一步缩进主体就可以工作。
添加in解决了这个问题,因为这会更明确地告诉编译器如何解释代码(因此它不依赖于缩进规则)。
另外,在一些单元测试框架中,也可以使用module,它为你提供了更轻量级的语法(但如果你需要一些初始化--比如加载文件,我不确定它是如何工作的):
[<TestFixture>]
module ``Given a valid file`` =
let myFile = createSomeFile()
[<Test>]
let ``Processing the file succeeds`` () =
let actual = processFile myFile
actual |> should be True https://stackoverflow.com/questions/10649696
复制相似问题