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

Castle.DynamicProxy.InvalidProxyConstructorArgumentsException when attempting to Setup or Verify based on a value returned from a mocked property #809

Closed
vgriph opened this issue Apr 16, 2019 · 1 comment · Fixed by #814

Comments

@vgriph
Copy link

vgriph commented Apr 16, 2019

Moq version: 4.10.1
When using a property from a mocked object as parameter to the expression passed to a Setup() or Verify() and the type of that property is missing a default constructor, an Castle.DynamicProxy.InvalidProxyConstructorArgumentsException is thrown.

It works if using an expression to read the value, or storing the value in a temporary variable, but not by using it directly from the mocked instance.

See the following test code. (Unsing NUnit)

    [TestFixture]
    public class ReproduceError
    {
        [Test]
        public void Failing()
        {
            var moq1 = new Mock<IProperty>();
            var moq2 = new Mock<IMethod>();
            var ndc = new ClassWithoutDefaultConstructor(string.Empty);

            moq1.Setup(x => x.Value).Returns(ndc);
            var mockedObject1 = moq1.Object;
            moq2.Setup(x => x.Test(mockedObject1.Value));
        }

        [Test]
        public void Working()
        {
            var moq1 = new Mock<IProperty>();
            var moq2 = new Mock<IMethod>();
            var ndc = new ClassWithoutDefaultConstructor(string.Empty);

            moq1.Setup(x => x.Value).Returns(ndc);
            var mockedObject1 = moq1.Object;
            var func = new Func<IProperty, ClassWithoutDefaultConstructor>(x => x.Value);
            moq2.Setup(x => x.Test(func(mockedObject1)));
        }

        public class ClassWithoutDefaultConstructor
        {
            public ClassWithoutDefaultConstructor(string dummy)
            {
            }
        }

        public interface IProperty
        {
            ClassWithoutDefaultConstructor Value { get; }
        }

        public interface IMethod
        {
            void Test(ClassWithoutDefaultConstructor value);
        }
    }
@stakx
Copy link
Contributor

stakx commented Apr 16, 2019

@vgriph, thanks for taking the time to report this. That usage scenario is already fixed in the current master branch by the recent work done in #767 and #782. Please feel free to submit a PR to add your above tests to the regression tests (in tests/Moq.Tests/Regressions/IssueReportsFixture.cs)!

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

Successfully merging a pull request may close this issue.

2 participants