2012-05-18 20 views
1

我們正在使用美妙的FSUnit進行單元測試。這工作正常,除了我們測試的主體堅持使用完整的F#語法(在每行的結尾處使用'in')而不是#light語法。例如:如何使用F#完整語法阻止我的FSUnit測試

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)。什麼觸發測試模塊中的完整語法?

回答

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 
+0

Thanks Tomas。在問題10分鐘內完成一個完整的多段落答案。 :-) – Kit