F# - 如何调用 Moq ReturnsAsync()?
F# - How to call Moq ReturnsAsync()?
我有一个函数 returns Task<bool>
(C#)。
Task<bool> Update(MemberMarketUpdate memberMarketUpdate);
我正在尝试使用 Moq (F#) 模拟它。
let verifyUpdate(update:MemberMarketUpdate) =
update.Id |> should equal "market id"
let setup = associationRepository.Setup (fun r -> r.Update(It.IsAny<MemberMarketUpdate>() ))
setup.Callback(fun update -> verifyUpdate update) |> ignore
//setup.Returns(Task.FromResult(True)) <- does not compile
//setup.ReturnsAsync(True) <- does not compile
我无法使用 ReturnsAsync(True)
模拟 Task<bool>
结果。
错误说:
No overloads match for method ReturnsAsync. The available overloads
are shown below.
(35 overloads here)
正确的语法是什么?
我对 f# 不是很熟悉,但如果可能的话,像下面这样在一个流畅的调用中尝试全部。
associationRepository
.Setup(fun r -> r.Update(It.IsAny<MemberMarketUpdate>()))
.Callback(fun update -> verifyUpdate update)
.ReturnsAsync(True) |> ignore
我试图让你的代码在一个简单的 F# 脚本文件中进行类型检查,我唯一要做的就是将你的大写 True
更改为小写 true
.
我的完整脚本可以进行类型检查,包括所有必要的定义和引用,如下所示:
#r @"packages\Moq\lib\net45\Moq.dll"
#r @"packages\NUnit\lib\net45\nunit.framework.dll"
#r @"packages\FsUnit\lib\net46\FsUnit.NUnit.dll"
#r @"packages\System.Threading.Tasks.Extensions\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll"
open Moq
open FsUnit
open System.Threading.Tasks
type MemberMarketUpdate =
{ Id: string }
type IAssociationRepository =
abstract Update : MemberMarketUpdate -> Task<bool>
let verifyUpdate(update:MemberMarketUpdate) =
update.Id |> should equal "market id"
let associationRepository = new Mock<IAssociationRepository>();
let setup = associationRepository.Setup (fun r -> r.Update(It.IsAny<MemberMarketUpdate>() ))
setup.Callback(fun update -> verifyUpdate update) |> ignore
setup.Returns(Task.FromResult(true))
setup.ReturnsAsync(true)
我有一个函数 returns Task<bool>
(C#)。
Task<bool> Update(MemberMarketUpdate memberMarketUpdate);
我正在尝试使用 Moq (F#) 模拟它。
let verifyUpdate(update:MemberMarketUpdate) =
update.Id |> should equal "market id"
let setup = associationRepository.Setup (fun r -> r.Update(It.IsAny<MemberMarketUpdate>() ))
setup.Callback(fun update -> verifyUpdate update) |> ignore
//setup.Returns(Task.FromResult(True)) <- does not compile
//setup.ReturnsAsync(True) <- does not compile
我无法使用 ReturnsAsync(True)
模拟 Task<bool>
结果。
错误说:
No overloads match for method ReturnsAsync. The available overloads are shown below.
(35 overloads here)
正确的语法是什么?
我对 f# 不是很熟悉,但如果可能的话,像下面这样在一个流畅的调用中尝试全部。
associationRepository
.Setup(fun r -> r.Update(It.IsAny<MemberMarketUpdate>()))
.Callback(fun update -> verifyUpdate update)
.ReturnsAsync(True) |> ignore
我试图让你的代码在一个简单的 F# 脚本文件中进行类型检查,我唯一要做的就是将你的大写 True
更改为小写 true
.
我的完整脚本可以进行类型检查,包括所有必要的定义和引用,如下所示:
#r @"packages\Moq\lib\net45\Moq.dll"
#r @"packages\NUnit\lib\net45\nunit.framework.dll"
#r @"packages\FsUnit\lib\net46\FsUnit.NUnit.dll"
#r @"packages\System.Threading.Tasks.Extensions\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll"
open Moq
open FsUnit
open System.Threading.Tasks
type MemberMarketUpdate =
{ Id: string }
type IAssociationRepository =
abstract Update : MemberMarketUpdate -> Task<bool>
let verifyUpdate(update:MemberMarketUpdate) =
update.Id |> should equal "market id"
let associationRepository = new Mock<IAssociationRepository>();
let setup = associationRepository.Setup (fun r -> r.Update(It.IsAny<MemberMarketUpdate>() ))
setup.Callback(fun update -> verifyUpdate update) |> ignore
setup.Returns(Task.FromResult(true))
setup.ReturnsAsync(true)