Skip to content

Instantly share code, notes, and snippets.

@theburningmonk
Created December 14, 2015 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theburningmonk/438ebe15b99ffd635da2 to your computer and use it in GitHub Desktop.
Save theburningmonk/438ebe15b99ffd635da2 to your computer and use it in GitHub Desktop.
public interface IDynamoDataAccessLayer
{
void Save<T>(T entity, string config = null);
void Save2<T>(T entity);
void Save3(string entity);
}
let dal =
Mock<IDynamoDataAccessLayer>()
.Setup(fun x -> <@ x.Save(any(), any()) @>)
.Calls<_>(fun x -> printfn "%A" x)
.Setup(fun x -> <@ x.Save2(any()) @>)
.Calls<_>(fun x -> printfn "%A" x)
.Setup(fun x -> <@ x.Save3(any()) @>)
.Calls<_>(fun x -> printfn "%A" x)
.Create()
dal.Save(42) // doesn't work
dal.Save2(42) // doesn't work
dal.Save3("42") // works
// tried this (explicit type for Call<int>) as well, same thing
let dal =
Mock<IDynamoDataAccessLayer>()
.Setup(fun x -> <@ x.Save(any(), any()) @>)
.Calls<int>(fun x -> printfn "%d" x)
.Setup(fun x -> <@ x.Save2(any()) @>)
.Calls<int>(fun x -> printfn "%d" x)
.Setup(fun x -> <@ x.Save3(any()) @>)
.Calls<_>(fun x -> printfn "%A" x)
.Create()
dal.Save(42) // doesn't work
dal.Save2(42) // doesn't work
dal.Save3("42") // works
@ptrelford
Copy link

Current Foq (1.7.1) requires explicitly typed arguments in the Setup if you are using Calls, otherwise the Setup produces an instance specialised to obj. This is so that Foq can support multiple Setups with different explicit generic arguments and match them accordingly.
This works in the current version:

type IDynamoDataAccessLayer =
interface
abstract Save<'T> : entity:'T * config:string -> unit
abstract Save2<'T> : entity:'T -> unit
abstract Save3 : entity:string ->unit
end

let dal =
Mock()
.Setup(fun x -> <@ x.Save(It.IsAny(), any()) @>)
.Calls<int*string>(fun x -> printfn "1 %A" x)
.Setup(fun x -> <@ x.Save2(any()) @>)
.Calls(fun x -> printfn "2 %A" x)
.Setup(fun x -> <@ x.Save3(any()) @>)
.Calls(fun x -> printfn "3 %A" x)
.Create()

dal.Save(42, "") // this work
dal.Save2(box 43) // this work
dal.Save3("42") // works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment