tonerdo / pose

Replace any .NET method (including static and non-virtual) with a delegate
MIT License
1.08k stars 74 forks source link

"InvalidProgramException: Common Language Runtime detected an invalid program." for .NetCore #49

Open WillRock19 opened 5 years ago

WillRock19 commented 5 years ago

Hello, I'm trying to use Pose to mock the property "Entitites" in the folowing class:

image

In my test, I'm doing the setup as follows:

image

But it throws the exception: System.InvalidProgramException : Common Language Runtime detected an invalid program

I've seen that other people has gone to the same problem, but usually involving DateTime.Now or something like it, and, still, could not find any solution. Do you guys are currently working on this?

jmoralesv commented 5 years ago

Hi all, I'm also getting that kind of exceptions when using a shim in a unit test, when using XUnit, Moq, Autofixture in a .NET Core 2.1 project, working in Visual Studio 2019 Enterprise.

I think there is a compatibility issue with .NET Core 2.x, as other people is complaining about the same kind of errors in other threads.

I was expecting to get a replacement for Microsoft Fakes that can work with .NET Core 2.x, however it looks like we won't be able to do that yet.

If anyone knows if there are other libraries that can help us define shims and stubs like Microsoft Fakes does, please let me know.

Regards, Jorge

oddbear commented 5 years ago

There might be some Fody plugins that can be used. However these works by changing the IL in the assemblies during build and inject code, so not exactly the same.

jmoralesv commented 5 years ago

Hi @oddbear, I've just checked Fody in GitHub and yeah, it's a broader set of tools to change IL while building .NET assemblies, which I think it is very interesting although that is not what we want to do here. Maybe the maintainer of Pose could take a look at Fody, but I think he abandoned Pose, as far as I can see.

I was trying to look for a library that can be used instead of MS Fakes, and Pose looked promising but seems to be broken. Hopefully Microsoft can bring MS Fakes to .NET Core when .NET Core 3.0 is available in September 2019.

Let's wait and see :confused:

oddbear commented 5 years ago

@jmoralesv then you have some options like JustMock and TypeMock, but they are sadly quite costly. As I understand they are both using the Profiling API, so they also work a little bit different.

Mentioned here https://github.com/tonerdo/pose/issues/51 is that the author started working for Microsoft, maybe this mean something will come out one day (however MS has killed other projects before this way, like Code Connect's Alive)?

For MS Fakes as it's now has a lot of limitations (making it unusable for most):

I would consider MS Fakes a dead project today, and hope Microsoft will release something soon that works for everyone. Or pose comes back alive, or maybe JetBrains will come up with something cheaper (seems like DotMemory Unit has some of the hard work figured out already).

Right now there is sadly a open gap in the marked.

riezebosch commented 5 years ago

Maybe you can give my little framework a try: https://github.com/riezebosch/Unmockable. Instead of fancy IL rewriting or profiler invocation redirection it simply wraps execution in an expression with a type that can be replaced with injection.