2015-02-11 76 views
0

我正在使用VS2013 Professional,F#, C#Nunit。值得注意的是,这是我第一次尝试F#,所以问题很可能很愚蠢,解决方案很明显。open NUnit.Framework - 命名空间或模块未定义。在F#中引用

我想要做的是使用NUnit实现测试用例,并使用TestCaseSource属性与TestCaseData

测试:

namespace Legal.Tests.Helpers 
open System 
open System.Collections.Generic 
open System.Linq 
open System.Text 
open System.Threading.Tasks 
open NUnit.Framework 
open Legal.Website.Infrastructure 

type FTest() = 
    [<TestFixture>] 
    [<TestCaseSource("FTestData")>] 
    let ConcatTest(text : string, expected : string) = 
    [<Test>] 
     let actual = Saga.Services.Legal.Website.Infrastructure.FunctionModule.TestFunction text 
     Assert.AreEqual expected actual 
    let FTestData : seq<TestCaseData> = [ new TestCaseData (text = "x", expected = "Item1xItem2"); new TestCaseData (text = "y", expected = "Item1yItem2") ] 

功能测试:

namespace Legal.Website.Infrastructure 

open System 
open System.Collections.Generic 
open System.Linq 
open System.Web 

type Test(text2 : string) = 
    member this.Text = "Item1" 
    member this.Text2 = text2 

module functions = 
    let TestFunction (text : string) = 
     let test = new Test (text2 = "Item2") 
     String.Concat [test.Text; text; test.Text2] 

有一件事值得一提的 - 我已经创建F#测试文件,并通过重命名.cs文件.fs与功能文件。

问题:当我尝试open不是System任何库(在这种情况下NuGet包NUnit.Framework和引用的项目Legal.Website.Infrastructure)我得到错误:在同一个目录下运行良好the namespace or module is not defined都在测试项目中引用和.cs测试。

+0

不知道我的理解,但 - 我不认为你可以重命名'.cs'文件到'.fs'文件 - 你需要为此创建一个新的F#项目。 – 2015-02-11 12:33:02

+0

@TomasPetricek所以我不能在C#项目中使用.fs文件,例如.vb文件。 – 2015-02-11 12:35:08

+0

@TomasPetricek你可以请你的评论作为答案,所以我可以接受它。谢谢。 – 2015-02-12 12:22:38

回答

1

我的问题很愚蠢。

.fs文件无法添加到C#项目中,例如.vb文件。要正确执行此操作,需要将F#项目添加到解决方案,请参阅下面的屏幕截图。

enter image description here

实现:

module Implementation 

let Concat(text:string) = 
    "root"+ text 

测试:

module Test 
open NUnit.Framework 

[<TestFixture>] 
type Test() = 
    member this.ConcatinationTestData() = 
     [new TestCaseData("roottext","text"); new TestCaseData("root","")] 
    [<Test>] 
    [<TestCaseSource("ConcatinationTestData")>] 
    static member ConcatinationTest(expected:string, text:string) = 
     Assert.AreEqual(expected,Implementation.Concat(text)) 
     |> ignore 

调试 - >附加到进程 - > NUnit的-agent.exe

结果: enter image description here