1

We are using the wonderful FSUnit for our unit testing. This works fine, except the bodies of our tests insist on using full F# syntax (with 'in' at the end of each line etc.) instead of #light syntax. For example:

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

Note the 'in' at the end of the first line of the test. Without that, the test won't compile - which is fine for short tests but is becoming a pain for longer test methods. We've tried adding an explicit #light in the source but that seems to make no difference. This is part of a large project with many modules, all of which - other than the test modules - are happily using light syntax (without any explicit #light). What's triggering full syntax in the test modules?

4

1 回答 1

2

在编写类成员时,您需要使用一些不同的缩进。以下应该没问题:

[<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 
于 2012-05-18T09:14:03.457 回答