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 extension methods #189

Closed
Hosch250 opened this issue Jul 11, 2015 · 1 comment
Closed

Mocking extension methods #189

Hosch250 opened this issue Jul 11, 2015 · 1 comment

Comments

@Hosch250
Copy link

I am working on an addin for the VBE - Rubberduck. We are working on getting everything under unit tests, but Moq doesn't support extension methods. Rather than implementing an interface between the VBE and RD just to stick our extension methods in, I figured I would write a patch for Moq that would allow interface methods. Looking through the project, this is the relevant code:

private static void ThrowIfNotMember(Expression setup, MethodInfo method)
{
    if (method.IsStatic)
    {
        throw new NotSupportedException(string.Format(
        CultureInfo.CurrentCulture,
        Resources.SetupOnNonMemberMethod,
        setup.ToStringFixed()));
    }
}

It is called just twice - in internal static MethodCallReturn<T, TResult>() and internal static MethodCall<T> Setup<T>().

I didn't dare touch this code because I don't know what bugs this is preventing. Could someone who knows explain what I would need to do to fix this?

Edit:

After a bit of research, I found the commit it was introduced in.

@kzu kzu closed this as completed Jul 11, 2015
@kzu
Copy link
Contributor

kzu commented Jul 11, 2015

You can't mock extension methods because they are static, and you can't
override a static method call by inheriting its declaring class.
On Sat, Jul 11, 2015 at 1:16 AM Hosch250 notifications@github.com wrote:

I am working on an addin for the VBE - Rubberduck
https://github.com/rubberduck-vba/Rubberduck. We are working on getting
everything under unit tests, but Moq doesn't support extension methods.
Rather than implementing an interface between the VBE and RD just to stick
our extension methods in there, I figured I would write a patch for Moq
that would allow interface methods. Looking through the project, this is
the relevant code:

private static void ThrowIfNotMember(Expression setup, MethodInfo method)
{
if (method.IsStatic)
{
throw new NotSupportedException(string.Format(
CultureInfo.CurrentCulture,
Resources.SetupOnNonMemberMethod,
setup.ToStringFixed()));
}
}

It is called just twice - in internal static MethodCallReturn<T,
TResult>() and internal static MethodCall Setup().

I didn't dare touch this code because I don't know what bugs this is
preventing. Could someone who knows explain what I would need to do to fix
this? It appears to have been committed just 24 days ago
4d0cb13
.


Reply to this email directly or view it on GitHub
#189.

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