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

Mocking derived classes of Exception results in .Object being null #1337

Closed
KWodarczyk opened this issue Mar 22, 2023 · 4 comments
Closed

Mocking derived classes of Exception results in .Object being null #1337

KWodarczyk opened this issue Mar 22, 2023 · 4 comments

Comments

@KWodarczyk
Copy link

KWodarczyk commented Mar 22, 2023

    public class myClass : Exception
    {

    }

            var obj = new Mock<myClass>().Object;// obj will be null why ?
@KWodarczyk KWodarczyk changed the title Mocking subclass of exception results in .Object being null Mocking derived classes of Exception results in .Object being null Mar 22, 2023
@stakx
Copy link
Contributor

stakx commented Mar 23, 2023

I cannot reproduce this problem.

using System;
using Moq;

Console.WriteLine(new Mock<myClass>().Object is Exception);  // outputs "True"

public class myClass : Exception { }

Everything works as it should, at least with the current version of Moq (4.18.4).

Btw. do you even need to mock Exception? Or could you perhaps just... instantiate one using new?

@KWodarczyk
Copy link
Author

KWodarczyk commented Mar 23, 2023

What happens if you remove "is Exception" btw I'm using windows apps SDK and .net 6
Also assign it to var first as I did

@stakx
Copy link
Contributor

stakx commented Mar 26, 2023

You can try that yourself.

But I guess the problem doesn't lie with the code itself, but with the targeted platform. I don't know Windows App SDK (Microsoft keeps releasing a new app platform SDK once every two years or so, I've given up trying to keep up), any chance your code gets compiled ahead-of-time (AOT) / compiled to native code / Ready-to-Run (R2R) / whatever else it's called these days?

If so, you're out of luck as Moq v4 & DynamicProxy, the library it's based on, heavily relies on Reflection, which may not be fully supported when compiling to native code.

@stakx
Copy link
Contributor

stakx commented Jul 20, 2023

@KWodarczyk, I am going to close this issue due to missing repro code. If you want this to be looked into any further, please provide instructions how to reliably repro this issue and we can reopen.

@stakx stakx closed this as completed Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants