使用选项初始化记录时,为什么会收到错误?
--下面一行失败了我的单元测试:
let name = { First=String20("Scott"); Last=String20("Nimrod"); Suffix=None }测试结果:
结果StackTrace: at CreateModuleViewModel.Tests.submit模块()结果消息: System.MissingMethodException :方法未找到:‘voidName..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
}为什么我会收到这个错误?
发布于 2016-01-24 00:13:44
MissingMethodException最常见的原因是您的一些依赖项是针对不同于单元测试库的不同版本的FSharp.Core.dll编译的。
解决这个问题的方法是将bindingRedirect添加到app.config中。我认为大多数单元测试运行程序也会尊重绑定重定向,因此这将解决问题。
马克·西曼·有一篇关于这个的博文。偷了他的榜样,你需要这样的东西:
<?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 2013)或4.4.0.0 (Visual 2015)。我将这里的oldVersion更改为一个应该包含可能存在的所有版本的范围。
这导致MethodMissingException的原因有点微妙--但如果没有重定向,运行时的事情(例如来自一个F# Core的option<T> )与来自另一个版本的F# Core的option<T>并不是一回事,因此它无法找到它所期望的方法。
https://stackoverflow.com/questions/34970587
复制相似问题