You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While mocking a type's member is super convenient, I found no way to mock a bare function, not hosted in any type. Or, at least, I was able to mock it, but assertions seem fail.
Replacing a function with a test double requires no library: it is just a matter of defining the replacing function and using it instead of the original one.
My goal, though, is then verify if the function was called and with which arguments.
When I try, I get a
Repro steps
I can easiliy replace a method with:
typeMyInterface=abstractCallMe: string :string->string[<Fact>]let``mock a method``()=letmock= Mock.Of<MyInterface>()
verify <@ mock.CallMe "Hello"@> never
mock.CallMe "Hello"
verify <@ mock.CallMe "Hello"@> once
If I try to do the same with a function:
typeMyFunc= string -> string
[<Fact>]let``mock a naked function``()=letmock= Mock.Of<MyFunc>()
verify <@(mock "Hello")@> never
mock "Hello"
verify <@(mock "Hello")@> once
I get a:
System.NotSupportedException
Expected function application: ValueWithName (<fun:FSharpFunc`257856397-a910-4815-8fa8-bb79a503eb1e>, mock)
at Foq.Reflection.unwrap@736(FSharpFunc`2toArgs',FSharpList`1values,FSharpExprquote)atFoq.Reflection.unwrap@736-1.Invoke(FSharpList`1values,FSharpExprquote)atFoq.Mock.DoVerify(FSharpExprexpr,TimesexpectedTimes)atSAI.AbsenceCollector.Test.Core.mock a naked function()inC:\Core.fs:line 35
Description
While mocking a type's member is super convenient, I found no way to mock a bare function, not hosted in any type. Or, at least, I was able to mock it, but assertions seem fail.
Replacing a function with a test double requires no library: it is just a matter of defining the replacing function and using it instead of the original one.
My goal, though, is then verify if the function was called and with which arguments.
When I try, I get a
Repro steps
I can easiliy replace a method with:
If I try to do the same with a function:
I get a:
The failure occurs at
Expected behavior
I expected both the asserts to pass.
Actual behavior
A
System.NotSupportedException
.Known workarounds
I found none.
Related information
There is a discussion on this topic on StackOverflow's Using Foq With F# Function Types
Any hints?
The text was updated successfully, but these errors were encountered: