给定具有FSharp样式函数的接口.
type IUseless =
abstract member Listify: string -> int -> string list
Run Code Online (Sandbox Code Playgroud)
你会如何嘲笑这个功能?
let substitute = NSubstitute.Substitute.For<IUseless>()
substitute.Listify.Returns(??? what would even go here ???)
Run Code Online (Sandbox Code Playgroud)
我不希望能够像普通方法一样模拟它,或者包含函数的值(虽然这就是它代表的东西).
所以我很好奇是否有人使用典型的.NET模拟库成功模拟了FSharp函数.
第一:是的,你可以像普通方法一样完全嘲笑:
let substitute = NSubstitute.Substitute.For<IUseless>()
(substitute.Listify "abc" 5).Returns ["whatevs"]
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为F#编译此定义就像普通的.NET方法一样,尽管语法模糊.这部分是为了互操作而部分是为了表现.
但第二:如果我是你,我宁愿NSubstitute完全跳过整个业务,而是使用内联接口实现:
let substitute = { new IUseless with member x.Listify a b = ["whatevs"] }
Run Code Online (Sandbox Code Playgroud)
这是更干净,更好的类型检查,并且在运行时更快.