Skip to content

Can't mock an abstract class with an "internal set" virtual property #27

@davidgruar

Description

@davidgruar

Description

Foq throws an exception if you try to mock a C# abstract class containing a virtual property with an internal setter.

Repro steps

Create the following class in a C# project:

public abstract class AbstractClass
{
    public int ConcreteInternalSet { get; internal set; }
    public virtual int VirtualPrivateSet { get; private set; }
    public virtual int VirtualProtectedSet { get; protected set; }
    public virtual int VirtualInternalSet { get; internal set; }
    public abstract int AbstractReadOnly { get; }
}

Create this test in an F# test project:

[<Fact>]
let abstractClassTest() =
    Mock<AbstractClass>().Create() |> ignore

Expected behavior

Test should pass.

Actual behavior

Fails with this error:

System.TypeLoadException : Method 'set_VirtualInternalSet' on type 'Mock.AbstractClassefa0bfaa-f64d-4c8b-b1b2-7eef47cfe38a' from assembly 'Foq.Dynamic, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is overriding a method that is not visible from that assembly.

Stack Trace:

   at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type)
   at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
   at System.Reflection.Emit.TypeBuilder.CreateTypeInfo()
   at Foq.Emit.mock(MockMode mode, Type abstractType, FSharpList`1 otherTypes, FSharpList`1 calls, Object[] args, FSharpOption`1 returnStrategy)
   at Foq.Mock`1.Create()

If you remove the VirtualInternalSet property, the test passes.

Related information

  • Windows 10
  • .NET Core 2.1
  • Foq 1.8.0
  • XUnit 2.4.0

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