Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
davidgruar opened this issue Mar 15, 2019 · 2 comments
Open

Comments

@davidgruar
Copy link

davidgruar commented Mar 15, 2019

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
@richardjharding
Copy link

Have just hit this when trying to use Foq to mock the Microsoft.Azure.WebjobsDurableOrchestrationContextBase type - I was attempting port the sample here https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-unit-testing#unit-testing-orchestrator-functions to use Foq

It should be noted that I can use Moq in f# to re-create that sample and mock the type without issue but I really like the use of quotations in Foq!

@belcher-rok
Copy link

belcher-rok commented Apr 11, 2022

Any progress on this issue? I just ran into it after upgrading to latest Azure.Storage.Blobs library (12.11.0). The Azure.Response type can no longer be mocked:

let resp = Foq.Mock<Azure.Response>().Create()

Fails at runtime with System.TypeLoadException : Method 'set_IsError' on type 'Mock.Responseda15e24f-4b36-4ebb-a375-a1c2b3ef1ec8' from assembly 'Foq.Dynamic, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is overriding a method that is not visible from that assembly.

Edit: I just noticed that the IsError property is not even virtual. Now I'm really confused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants