Skip to content

Unit type return results in InvalidProgramException #23

@drivehappy

Description

@drivehappy

Description

Building a mock for a member that returns a unit type results in a System.InvalidProgramException: "Common Language Runtime detected an invalid program."

Repro steps

Compile and run the following program:

open Foq

type IMyInterface =
    abstract member foo: int -> unit

let mockMyInterface = Mock<IMyInterface>.With(fun mock ->
    <@ mock.foo(any()) --> () @>
)

[<EntryPoint>]
let main args = 
    mockMyInterface.foo(5)
    0 

Expected behavior

The mock should successfully call foo(5) without error.

Actual behavior

An exception of System.InvalidProgramException is thrown.

Known workarounds

It seems if I build the mock out via the Setup/Create approach this works correctly:

let mockMyInterface = 
    Mock<IMyInterface>()
        .Setup(fun x -> <@ x.foo() @>)
        .Returns(())
        .Create()

The documentation is sparse, so I'm unsure of the difference between the two (I opted for the .With() for brevity).

Related information

  • Windows 10 x64
  • For 1.7.2
  • .NET 4.5/4.7 Framework causes this exception. Mono 5.x fails with a stacktrace and the message unknown type 0x01 in type_to_load_membase

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions