NSubstitute and FSharp - Offset to FSharp function

For an interface that has a FSharp style feature.

type IUseless =
    abstract member Listify: string -> int -> string list

      

How would you mock the function?

let substitute = NSubstitute.Substitute.For<IUseless>()
substitute.Listify.Returns(??? what would even go here ???)

      

I would not expect to be able to mock it as a normal method or a value containing a function (although that is what it represents).

So I'm curious if anyone has successfully mocked the FSharp function with a typical .NET mocking library.

+3


source to share


1 answer


First: yes, you can completely mock this like a normal method:

let substitute = NSubstitute.Substitute.For<IUseless>()
(substitute.Listify "abc" 5).Returns ["whatevs"]

      

This works because F # compiles this definition as a regular .NET method, despite the curry syntax. This is done partly for interoperability and partly for performance.



But the second one: if I were you, I'd rather skip the whole business entirely NSubstitute

and use the built-in interface implementation instead:

let substitute = { new IUseless with member x.Listify a b = ["whatevs"] }

      

It's cleaner, better typewriter, and much faster at runtime.

+6


source







All Articles