Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't stub class with NullableContextAttribute

Tags:

c#

rhino-mocks

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 
like image 356
Stealth Rabbi Avatar asked Sep 22 '20 13:09

Stealth Rabbi


2 Answers

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.

like image 122
Alexander Avatar answered Oct 16 '22 06:10

Alexander


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.

like image 45
rabindra Avatar answered Oct 16 '22 07:10

rabindra