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

Parameter Array modifier lost in mock #235

Closed
afscrome opened this issue Jan 2, 2016 · 4 comments
Closed

Parameter Array modifier lost in mock #235

afscrome opened this issue Jan 2, 2016 · 4 comments

Comments

@afscrome
Copy link

afscrome commented Jan 2, 2016

The only way to detect through reflection if a method parameter is a parameter array is by checking if the parameter has the ParamArrayAttribute attribute defined on it. However when you mock a method with a params array, the mock implementation looses this attribute.

This behavior can be shown by the below test - I'd expect the last assertion to pass, but it currently fails.

public abstract class Base
{
    public abstract string ParamsArray(params int[] args);
}

[Test]
public void ParamsArrayLostOnMock()
{
    var baseMethodParam = typeof(Base).GetMethods()[0].GetParameters()[0];

    var mock = new Mock<Base>();
    mock.Setup(x => x.ParamsArray(1, 2, 3)).Returns("foo");

    var mockMethodParam = mock.Object.GetType().GetMethods()[0].GetParameters()[0];

    Assert.IsTrue(baseMethodParam.IsDefined(typeof(ParamArrayAttribute), false), "ParamArrayAttribute is not defined on base");
    Assert.IsTrue(mockMethodParam.IsDefined(typeof(ParamArrayAttribute), false), "ParamArrayAttribute is not defined on mock");
}
@kzu
Copy link
Member

kzu commented Jan 2, 2016

This is an issue in Castle.DynamicProxy most likely. Can you see if it can be reproduced with any of the other open source mocking libraries? (I.e. Rhino, NSubstitute, FakeItEasy, etc.?)

@afscrome
Copy link
Author

afscrome commented Jan 2, 2016

I was able to reproduce with NSubstitue, as well as using ProxyGenerator directly - see castleproject/Core#121 .

@kzu
Copy link
Member

kzu commented Feb 21, 2017

Marking as an enhacement that has to happen in Castle.

@stakx
Copy link
Contributor

stakx commented Jun 11, 2017

A fix for this is going to be in Castle Core v4.1 (see castleproject/Core#121 (comment)). As soon as we upgrade Castle and republish Moq, this can be closed.

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

3 participants