2016-01-24 43 views
1

爲什麼使用選項初始化記錄時會收到錯誤?爲什麼使用選項初始化記錄時會收到錯誤?

以下行失敗我的單元測試:

let name =  { First=String20("Scott"); Last=String20("Nimrod"); Suffix=None } 

測試結果:

結果堆棧跟蹤:在CreateModuleViewModel.Tests.submit模塊() 結果消息:系統。 MissingMethodException:未找到方法: 'Void Name..ctor(String20,String20, Microsoft.FSharp.Core.FSharpOption`1)'。

測試如下:

module CreateModuleViewModel.Tests 

open FsUnit 
open NUnit.Framework 
open UILogic.State 
open CreateModule.UILogic 
open ManageModule.Entities 

[<Test>] 
let ``submit module``() = 

    // Setup 
    let viewModel = CreationViewModel() 

    let name =  { First=String20("Scott"); Last=String20("Nimrod"); Suffix=None } 

    let duration = { Hours=1; Minutes=30; Seconds=0 } 
    let moduleItem = { Author=name; Duration=duration } 

    // Tets 
    viewModel.Add(moduleItem) 

    // Verify 
    viewModel.Modules.Head = moduleItem |> should equal true 

記錄定義如下:

type String20 = String20 of string 

type Name = { 
    First:String20 
    Last:String20 
    Suffix:String20 option 
} 

我爲什麼會收到這個錯誤?

+2

測試和類型定義在不同的程序集中?如果是這樣,你確定在運行時調用了提供類型定義的正確版本的dll嗎?我沒有看到與代碼相關的原因讓您遇到此錯誤。 – TheInnerLight

+1

某些地方的程序集肯定是錯誤的版本。 –

+0

謝謝。我的測試項目設置爲F#3.1並鎖定到該版本,而我的其他所有庫都是F#4.0。 –

回答

4

MissingMethodException最常見的原因是,一些你依賴的是針對不同版本的FSharp.Core.dll比單元測試庫編譯。

解決此問題的方法是將bindingRedirect添加到您的app.config。我想大多數單元測試運行者都會尊重綁定重定向,所以這應該能夠解決這個問題。

Mark Seemann has a blog post about this。偷了他的例子,你需要的東西,如:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="FSharp.Core" 
          publicKeyToken="b03f5f7f11d50a3a" 
          culture="neutral"/> 
     <bindingRedirect oldVersion="0.0.0.0-99.99.99.99" 
         newVersion="4.3.1.0"/> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
</configuration> 

newVersion將是要麼4.3.1.0(Visual Studio中2013)或4.4.0.0(Visual Studio中2015年)。我將這裏的oldVersion更改爲應該包含所有版本的範圍。

這導致MethodMissingException的原因有點微妙 - 但沒有重定向,運行時的事情,例如,來自一個F#Core的option<T>與另一個F#Core版本的option<T>不是一回事,所以它找不到它期望的方法。

+0

感謝Tomas。我的測試現在通過。但是,我以前通過的其他測試現在都會失敗,並顯示相同的錯誤消息。我正在研究提供的博客,但尚未確定所需的應用程序配置解決所有dll衝突。有什麼建議麼? –

+0

將BindingRedirect的新版本屬性從newVersion =「4.3.1.0」更新爲:newVersion =「4.4.0.0」。我也遵循了TheInnerLight的建議,並更新了我的測試項目中的FSharp.Core dll以指向FSharp4.0。 –

+0

我不確定我瞭解你的配置是什麼樣的。其他測試是否在同一個項目中? –

相關問題