我会让你用Foq来模拟IBus。
IBus上的一个方法是OpenPublishChannel,它返回一个IPublishChannel。IPublishChannel又有一个返回父IBus的Bus属性。
我当前的代码如下,但很明显它不能编译,因为mockBus不是由我需要的地方定义的。有没有一种方法可以像这样设置递归模拟,而不需要为任何一个接口创建两个模拟?
open System
open EasyNetQ
open Foq
let mockChannel =
Mock<IPublishChannel>()
.Setup(fun x -> <@ x.Bus @>).Returns(mockBus)
.Create()
let mockBus =
Mock<IBus>()
.Setup(fun x -> <@ x.OpenPublishChannel() @>).Returns(mockChannel)
.Create()发布于 2013-04-23 18:58:29
Foq支持Returns : unit ->的TValue方法,因此您可以懒惰地创建值。
使用一个小的突变实例可以相互引用:
type IPublishChannel =
abstract Bus : IBus
and IBus =
abstract OpenPublishChannel : unit -> IPublishChannel
let mutable mockBus : IBus option = None
let mutable mockChannel : IPublishChannel option = None
mockChannel <-
Mock<IPublishChannel>()
.Setup(fun x -> <@ x.Bus @>).Returns(fun () -> mockBus.Value)
.Create()
|> Some
mockBus <-
Mock<IBus>()
.Setup(fun x -> <@ x.OpenPublishChannel() @>).Returns(fun () -> mockChannel.Value)
.Create()
|> Somehttps://stackoverflow.com/questions/16167068
复制相似问题