为什么在使用选项初始化记录时会收到错误消息?
Why do I receive an error when initializing a record with an option?
为什么我在使用选项初始化记录时收到错误消息?
以下行未通过我的单元测试:
let name = { First=String20("Scott"); Last=String20("Nimrod"); Suffix=None }
测试结果:
Result StackTrace: at CreateModuleViewModel.Tests.submit module()
Result Message: System.MissingMethodException : Method not found:
'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
}
为什么我收到这个错误?
MissingMethodException
的最常见原因是您的某些依赖项是针对与单元测试库不同的 FSharp.Core.dll
版本编译的。
解决这个问题的方法是将 bindingRedirect
添加到您的 app.config
。我认为大多数单元测试运行器也会尊重绑定重定向,因此这应该可以解决问题。
马克·西曼 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>
不同,因此它找不到它所期望的方法。
为什么我在使用选项初始化记录时收到错误消息?
以下行未通过我的单元测试:
let name = { First=String20("Scott"); Last=String20("Nimrod"); Suffix=None }
测试结果:
Result StackTrace: at CreateModuleViewModel.Tests.submit module() Result Message: System.MissingMethodException : Method not found: '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
}
为什么我收到这个错误?
MissingMethodException
的最常见原因是您的某些依赖项是针对与单元测试库不同的 FSharp.Core.dll
版本编译的。
解决这个问题的方法是将 bindingRedirect
添加到您的 app.config
。我认为大多数单元测试运行器也会尊重绑定重定向,因此这应该可以解决问题。
马克·西曼 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>
不同,因此它找不到它所期望的方法。