Skip to content

Instantly share code, notes, and snippets.

@ptrelford
Created November 19, 2013 07:36
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 ptrelford/7541673 to your computer and use it in GitHub Desktop.
Save ptrelford/7541673 to your computer and use it in GitHub Desktop.
Foq cannot explicitly return an F# type as null
module ``Foq cannot explicitly return an F# type as null``
open System
open Foq
// Discriminated Unions
type TimeInForce = GoodTillCancel | GoodTillDate of DateTime
type IOrder =
abstract LimitPrice : decimal
abstract Quantity : int
abstract TimeInForce : TimeInForce
let ``can setup member to return DU as value`` =
Mock<IOrder>().Setup(fun x -> <@ x.TimeInForce @>).Returns(GoodTillCancel)
let ``cannot setup member to return DU as null`` =
// Error: No overload match for method 'Returns'
Mock<IOrder>().Setup(fun x -> <@ x.TimeInForce @>).Returns(null)
let ``cannot mock with member returning DU as null`` =
// Error: The type 'TimeInForce' does not have 'null' as a proper value
Mock<IOrder>.With(fun x -> <@ x.TimeInForce --> null @>)
// Record types
type Counterparty = { Name : string; Company : string }
type ITrade =
abstract Price : decimal
abstract Quantity : int
abstract Initiator : Counterparty
abstract Aggressor : Counterparty
let ``can setup member to return record as value`` =
let initiator = { Name = "Name"; Company = "Company" }
Mock<ITrade>().Setup(fun x -> <@ x.Initiator @>).Returns(initiator)
let ``cannot setup member to return record as null`` =
// Error: No overload match for method 'Returns'
Mock<ITrade>().Setup(fun x -> <@ x.Aggressor @>).Returns(null)
let ``cannot mock with member returning record as null`` =
// Error: The type 'Counterparty' does not have 'null' as a proper value
Mock<ITrade>.With(fun x -> <@ x.Aggressor --> null @>)
// Tuples
type IMath =
abstract DivRem : float * float -> (float * float)
let ``can setup member to return tuple as value`` () =
Mock<IMath>().Setup(fun x -> <@ x.DivRem(any(),any()) @>).Returns((1.0,2.0))
let ``cannot setup member to return tuple as null`` () =
// Error: No overload match for method 'Returns'
Mock<IMath>().Setup(fun x -> <@ x.DivRem(any(),any()) @>).Returns(null)
let ``cannot mock with member returning tuple as null`` () =
// Error: The type '(float*float)' does not have 'null' as a proper value
Mock<IMath>.With(fun x -> <@ x.DivRem(any(),any()) --> null @>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment