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

System.InvalidCastException when invoking method that starts with "add" #82

Closed
F0b0s opened this issue Dec 25, 2013 · 8 comments
Closed

Comments

@F0b0s
Copy link

F0b0s commented Dec 25, 2013

Hello, I faced with the problem when i used Moq.
I wrote the simple test

public interface ILogger
{
    void add_info(string info);
    void Add_info(string info);
}

[TestFixture]
public class Class1
{
    [Test]
    public void Test_add_info()
    {
        var mock = new Mock<ILogger>();
        mock.Setup(x => x.add_info(It.IsAny<string>()));
        mock.Setup(x => x.Add_info(It.IsAny<string>()));

        mock.Object.add_info("asd");
        //mock.Object.Add_info("asd");
    }
}

When i call

mock.Object.add_info("asd");

it throws
System.InvalidCastException : Не удалось привести тип объекта "System.String" к типу "System.Delegate".
в Moq.AddActualInvocation.HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx)
в Moq.Interceptor.Intercept(ICallContext invocation)
в Castle.DynamicProxy.AbstractInvocation.Proceed()
в Test.Class1.Test_add_info() в Class1.cs: line 22

Sorry, i have Russian VS, it says: "Can't cast System.String to System.Delegate". But, when i call

mock.Object.Object.Add_info("asd");

string, it works.
I use VS 2012, .NET4.5. Nuget packages - NUnit, Moq(v.4.2.1312.1622). The Code was runned from Resharper test runner.

@MatKubicki
Copy link
Contributor

I can reproduce this and have tracked it down to a problem with the way Moq detects event handler attach methods and other special method types. The key file is:
\Source\MemberInfoExtensions.cs - in this case Line 66

Methods called 'add__' and 'remove__' will not work, this is case sensitive hence why 'Add' works. We will need to try and find a better way to detect if something is an event handler attach call.

@kzu
Copy link
Contributor

kzu commented Jan 2, 2014

Maybe we can just try to cast and fallback to considering it a regular method oherwise?

@ashmind
Copy link
Contributor

ashmind commented Feb 7, 2014

Shouldn't that be checking IsSpecialName?

@kzu
Copy link
Contributor

kzu commented Feb 10, 2014

+1 to IsSpecialName. Shouldn't pass for a normal method named "add_*"

@F0b0s
Copy link
Author

F0b0s commented Feb 10, 2014

Why we should restrict users in their naming style(i have a lot of legacy code and it might be painfull to change restricted names)? Can we mark all special method types with some attribute?

@kzu
Copy link
Contributor

kzu commented Feb 10, 2014

it's been so many years and this has come up so rarely that I'd say it's a
corner case which we're fine not supporting. As long as we don't throw any
exceptions, it should be fine.

I recall now that we removed the check for IsSpecialName for compatibility
with F#. See
44070a9

The naming goes against anything in .NET conventions for public/protected
members, so I think it's reasonable to assume it won't be (and hasn't been)
a serious issue for most.

/kzu

Daniel Cazzulino

On Sun, Feb 9, 2014 at 11:56 PM, F0b0s notifications@github.com wrote:

Why we should restrict users in their naming style(i have a lot of legacy
code and it might be painfull to change restricted names)? Can we mark all
special method types with some attribute?

Reply to this email directly or view it on GitHubhttps://github.com//issues/82#issuecomment-34600996
.

@sreniaw
Copy link

sreniaw commented Mar 26, 2014

Aha, I was mocking a web service with add_* method names and came across this issue :)

@stakx
Copy link
Contributor

stakx commented Dec 29, 2017

Regular methods with add_ or remove_ name prefixes are now supported in Moq 4.8.0.

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

6 participants