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

VerifySet throws TargetInvocationException when constructer refers to itself #1455

Open
philiphoy opened this issue Jan 5, 2024 · 2 comments
Labels

Comments

@philiphoy
Copy link

philiphoy commented Jan 5, 2024

The exception: System.Reflection.TargetInvocationException with the message " Exception has been thrown by the target of an invocation."

Is thrown during a call to VerifySet when code in a classes constructor references a property of this

Steps to Reproduce

This code reproduces the exception

       [TestMethod]
        public void Broken_moq()
        {
            var sut = Mock.Of<Example>();
            Mock.Get(sut).VerifySet(x => x.A = "A", Times.Never);
        }

        public class Example
        {
            public virtual string A { get; set; }
            public virtual string B { get; set; }
            public Example()
            {
                A = "";
                B = this.A;
            }
        }

Expected Behavior

The test above should run without throwing an exception

Exception with Stack Trace

 Recorder.Intercept(Invocation invocation) line 413
Interceptor.Intercept(IInvocation underlying) line 218
AbstractInvocation.Proceed()
ExampleProxy_1.get_A()
Example.ctor() line 124
ExampleProxy_1.ctor(IInterceptor[] )
--- End of inner exception stack trace ---
RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
ProxyGenerator.CreateClassProxyInstance(Type proxyType, List`1 proxyArguments, Type classToProxy, Object[] constructorArguments)
ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
CastleProxyFactory.CreateProxy(Type mockType, IInterceptor interceptor, Type[] interfaces, Object[] arguments) line 110
ActionObserver.CreateProxy(Type type, Object[] ctorArgs, MatcherObserver matcherObserver, Recorder& recorder) line 275
ActionObserver.ReconstructExpression[T](Action`1 action, Object[] ctorArgs) line 53
Mock`1.VerifySet(Action`1 setterExpression, Func`1 times) line 1236

Version Info

4.20.70

Back this issue
Back this issue

@philiphoy philiphoy added the bug label Jan 5, 2024
Copy link

Due to lack of recent activity, this issue has been labeled as 'stale'.
It will be closed if no further activity occurs within 30 more days.
Any new comment will remove the label.

@github-actions github-actions bot added the stale label Aug 24, 2024
@github-actions github-actions bot removed the stale label Sep 3, 2024
@kzu
Copy link
Member

kzu commented Sep 3, 2024

Invoking virtual members from a constructor is pretty much an anti-pattern.

There seems to be some support for that but I don't even know why supporting that (however imperfectly) would be a good thing, TBH.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants