You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have the following interfaces in the assembly ProjectA:
public interface IView {
void Method_1();
internal void InternalMethod();
}
public interface IViewModule {
IView Host();
}
Then in another project, ProjectB, that consumes ProjectA, I just have visibility to Method_1. That's fine and everything seems to work. Except for tests that are failing.
In ProjectB I have another interface that inherits from IView.
public interface IExtendedView : IView {
// Other methods
}
Test that fails
Now I will show you the code that will create the exception:
void MockViewModule() {
var viewModule = new Mock<IViewModule>();
var view = new Mock<IExtendedView>();
// It crashes in the next line
viewModule.SetupGet(x => x.Host).Returns(view.Object);
}
Error Message
Message:
System.TypeLoadException : Method 'InternalMethod' in type 'Castle.Proxies.IExtendedViewProxy_5' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.
Stack Trace:
TypeBuilder.CreateTypeNoLock()
TypeBuilder.CreateTypeInfo()
AbstractTypeEmitter.CreateType(TypeBuilder type)
AbstractTypeEmitter.BuildType()
InterfaceProxyWithoutTargetGenerator.GenerateType(String typeName, Type proxyTargetType, Type[] interfaces, INamingScope namingScope)
<>c__DisplayClass6_0.<GenerateCode>b__0(String n, INamingScope s)
BaseProxyGenerator.ObtainProxyType(CacheKey cacheKey, Func`3 factory)
InterfaceProxyWithTargetGenerator.GenerateCode(Type proxyTargetType, Type[] interfaces, ProxyGenerationOptions options)
DefaultProxyBuilder.CreateInterfaceProxyTypeWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
ProxyGenerator.CreateInterfaceProxyTypeWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, IInterceptor[] interceptors)
CastleProxyFactory.CreateProxy(Type mockType, IInterceptor interceptor, Type[] interfaces, Object[] arguments)
Mock`1.InitializeInstancePexProtected()
PexProtector.Invoke(Action action)
Mock`1.InitializeInstance()
Mock`1.OnGetObject()
Mock.get_Object()
Mock`1.get_Object()
I would like to understand if there's a way to avoid this exception.
The text was updated successfully, but these errors were encountered:
No, I'm not yet sure. (That's why I wrote "initial suspicion", it was more of a semi-informed guess aka gut feeling.) You can ignore my above post, it was mostly a reminder for myself for when I get time to look at this in detail. 🙂
@heldergoncalves92 – in case you haven't solved this yet yourself, you'll need to add the following somewhere in your ProjectA (the project that defines IView):
Alternatively, add a default implementation for InternalMethod directly in the interface, instead of only declaring it.
Reason: It isn't possible for DynamicProxy (the proxying library on top of which Moq 4 is built) to create a full implementation class of IView if InternalMethod isn't accessible to it. Once you make IView's internals accessible to DynamicProxy, it can then implement the method in a dynamically generated subtype (proxy class).
The InternalsVisibleTo attribute is explained a bit in the Quickstart.
Work State
Moq version: 4.10.0.0 (C#)
TargetFramework: net5.0
I have the following interfaces in the assembly
ProjectA
:Then in another project,
ProjectB
, that consumesProjectA
, I just have visibility toMethod_1
. That's fine and everything seems to work. Except for tests that are failing.In
ProjectB
I have another interface that inherits fromIView
.Test that fails
Now I will show you the code that will create the exception:
Error Message
I would like to understand if there's a way to avoid this exception.
The text was updated successfully, but these errors were encountered: