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

Substituting for COM interfaces #723

Closed
Tum4ik opened this issue Aug 12, 2023 · 5 comments
Closed

Substituting for COM interfaces #723

Tum4ik opened this issue Aug 12, 2023 · 5 comments

Comments

@Tum4ik
Copy link

Tum4ik commented Aug 12, 2023

During migration from Moq to NSubstitute I've come up with the problem of mocking COM interfaces. It worked with Moq, but now I get the exception:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : Cannot perform runtime binding on a null reference

It is possible to face with that case in my repository (branch: plugins).

So, is there something I don't know? Or maybe it requires additional development.

Thanks for help in advance.

@dtchepak
Copy link
Member

Hi @Tum4ik ,

I haven't had a chance to try this out yet, but one thing to try is using Configure which will prevent the substituted class from running any real code while it is being set up (you will need to add using NSubstitute.Extensions;):

 _wshShell.Configure().CreateShortcut(Arg.Any<string>()).Returns(shortcut);

If that doesn't work please let me know and I'll try to reproduce it locally.

@Tum4ik
Copy link
Author

Tum4ik commented Aug 13, 2023

Hi @dtchepak ,

Thank you for your response. Unfortunately, your suggestion didn't change anything. The exception is still present.

@dtchepak
Copy link
Member

@Tum4ik Thanks for trying it out.

This may take me a bit of time to work out sorry as I don't currently have access to a Windows machine.

Is there any more info you can provide about WshShell that may give us some clues?

We've had some previous issues with dynamic that might be related?

@Tum4ik
Copy link
Author

Tum4ik commented Aug 14, 2023

Oh... I have exactly this case.
So, I am able now to fix the problem via:

SubstituteExtensions.Returns((IWshShortcut) _wshShell.CreateShortcut(Arg.Any<string>()), shortcut);

Thank you, @dtchepak!

@Tum4ik Tum4ik closed this as completed Aug 14, 2023
@dtchepak
Copy link
Member

dtchepak commented Sep 3, 2023

Thanks for the update @Tum4ik . I don't normally work with dynamic so if you find a neater way for NSubstitute to support this case please let us know or send through a PR. 🙇

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

2 participants