I'm using a third party library which has some classes I'm trying to mock using Rhino Mocks. When I attempt to create a stub for a class, I'm getting the following error. I cna't find a reference to what NullableContextAttribute is, but I assume it's related to specifying a nullable type on an event/delegate definition somewhere. I can't figure out what an IAttributeDissassembler is.
I've read some solutions about using AttributesToAvoidReplicating
, but I'm not sure what class to give it. NullableContextAttribute
is not a class that can resolve to any namespace I can find.
I'm doing C#, .NET Framework 4.7.2. I tried changing my C# language level higher, but I think the nullable reference attribute is on something that is in c# 8 which I think I can't target with any .NET Framework? .
var myStub = MockRepository.GenerateStub<SomeOtherClass>();
Castle.DynamicProxy.ProxyGenerationException : There was an error trying to replicate non-inheritable attribute NullableContextAttribute using default attribute disassembler. Use custom implementation of IAttributeDisassembler (passed as 'AttributeDisassembler' property of ProxyGenerationOptions) to replicate this attribute.
at Castle.DynamicProxy.DefaultAttributeDisassembler.Disassemble(Attribute attribute)
at Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.DefineCustomAttribute(Attribute attribute, IAttributeDisassembler disassembler)
at Castle.DynamicProxy.Generators.BaseProxyGenerator.ReplicateNonInheritableAttributes(Type targetType, ClassEmitter emitter)
at Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateType(String newName, Type[] interfaces)
at Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateCode(Type[] interfaces, ProxyGenerationOptions options)
at Castle.DynamicProxy.DefaultProxyBuilder.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxyType(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
at Rhino.Mocks.MockRepository.MockClass(CreateMockState mockStateFactory, Type type, Type[] extras, Object[] argumentsForConstructor)
at Rhino.Mocks.MockRepository.Stub(Type type, Object[] argumentsForConstructor)
at Rhino.Mocks.MockRepository.<>c__DisplayClass1`1.<GenerateStub>b__0(MockRepository repo)
at Rhino.Mocks.MockRepository.CreateMockInReplay[T](Func`2 createMock)
at
Here is my dirty workaround. Since NullableContextAttribute
is only for the compiler, you can't use it in source code 1. But you can find this type in mocking assembly like this Array.Find(typeof(SomeOtherClass).Assembly.GetTypes(), t => t.Name == "NullableContextAttribute")
where SomeOtherClass is what you are trying to mock. Found Type
object you can pass to Castle.DynamicProxy.Generators.AttributesToAvoidReplicating.Add
.
I repeat, this is probably the worst solution, but it worked for me.
Changing GenerateStub
to GenerateStrictMockWithRemoting
worked for me in a similar situation. You can try:
var myStub = MockRepository.GenerateStrictMockWithRemoting<SomeOtherClass>();
Don't have the explanation though for why it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With