在C#中,我曾经写过
var provider = A.Fake<ITimeProvider>();
A.CallTo(() => provider.Fetch()).ReturnsLazily(call => data[0]);
container.Register(() => provider);
捕获对Fetch()
的调用。
当我尝试用F#
时let provider = A.Fake<ITimeProvider>()
A.CallTo(fun () -> provider.Fetch()).ReturnsLazily(fun call -> data.[0]) |> ignore
container.Register(fun () -> provider)
测试失败
Test Error : ....Test
System.ArgumentException : The specified object is not recognized as a fake object.
at Microsoft.FSharp.Control.AsyncBuilderImpl.commit[a](Result`1 res)
at Microsoft.FSharp.Control.CancellationTokenOps.RunSynchronously[a](CancellationToken token, FSharpAsync`1 computation, FSharpOption`1 timeout)
at Microsoft.FSharp.Control.FSharpAsync.RunSynchronously[T](FSharpAsync`1 computation, FSharpOption`1 timeout, FSharpOption`1 cancellationToken)
...
对于F#来说,它的定义应该有点不同。你有什么想法吗?
答案 0 :(得分:6)
FakeItEasy使用F#3支持的LINQ表达式,但在使用静态API时似乎存在不兼容性。基于错误消息“对象未被识别为伪对象”我怀疑在这种情况下,对象的分辨率是C#/ VB.Net特定的。
FakeItEasy的基于实例成员的设置确实可行:
let fake = Fake<ITimeProvider>()
fake.CallsTo(fun x -> x.Fetch()).ReturnsLazily(fun () -> data.[0]) |> ignore
let provider = fake.FakedObject
另一种选择是使用F#和Moq(这里我使用的是Moq.FSharp.Extensions):
let mock = Mock<ITimeProvider>()
mock.SetupFunc(fun x -> x.Fetch()).Returns(data.[0]).End
let provider = mock.Object
或者Foq,它是专为F#设计的:
let provider =
Mock<ITimerProvider>.Method(fun x -> <@ x.Fetch @>).Returns(data.[0])