1

I have to write some tests using moq framework (https://code.google.com/p/moq/). I want to check if some value can be read correctly from a telerik textbox in some ASP.NET application. Therefore I want to mock the telerik text box and give this mock as a parameter to the SUT's method to check if it is read correctly.

Consider:

var telerikFake = new Mock<RadNumericTextBox>();
telerikFake.Setup(x => x.Text).Returns("23,456");
var result = telerikFake.Object; //The exception comes from inside the telerikFake.Object property implementation

Accessing the telerikFake.Object property gives a NullReferenceException with this stack trace:

at Castle.DynamicProxy.AttributeUtil.<GetNonInheritableAttributes>d__0.MoveNext()
   at Castle.DynamicProxy.Contributors.ClassProxyInstanceContributor.Generate(ClassEmitter class, ProxyGenerationOptions options)
   at Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateType(String name, Type[] interfaces, INamingScope namingScope)
   at Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateCode(Type[] interfaces, ProxyGenerationOptions options)
   at Castle.DynamicProxy.DefaultProxyBuilder.CreateClassProxyType(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 Moq.Proxy.CastleProxyFactory.CreateProxy[T](ICallInterceptor interceptor, Type[] interfaces, Object[] arguments)
   at Moq.Mock`1.<InitializeInstance>b__0()
   at Moq.PexProtector.Invoke(Action action)

Do you have any hints under what circumstances the moq frameworks Mock.Object method leads to this error?

I found a ticket within the MoQ issue list: http://code.google.com/p/moq/issues/detail?id=326 . Are there any developers enabled to fix this issue soon? The ticket was created in 2011.

Regards, Michael

MoCapitan
  • 439
  • 4
  • 9
  • Not directly related to your question, but your test looks very strange. It looks like you have not only mocked the telerik textbox but also your SUT. Evidence: `_sutMock.Object.`. That makes no sense at all, because you are not testing any real code here. – Daniel Hilgarth May 06 '13 at 14:29
  • Oh, there is an issue on the MoQ issue list: https://code.google.com/p/moq/issues/detail?id=326 – MoCapitan May 06 '13 at 14:32
  • Hi Daniel! That's not the case because I use some partial mock {CallBase=true} for the sut because of some other requirements. I edited my source code to focus on the problem and not on the sut. Thanks for the hint. Michael – MoCapitan May 06 '13 at 14:39
  • Can you also include the exception message too? – Brian Mains May 06 '13 at 14:45
  • The exception message is "Object reference not set to an instance of an object." . It is raised inside the ".Object" getter of Moq with the stacktrace given above. – MoCapitan May 06 '13 at 15:03
  • 2
    Why do you need to mock the control at all? Does it have some side effects which you don't need? Why not just var control = new RadNumericTextBox(); control.Text = "123"; – Sunny Milenov May 06 '13 at 15:54
  • In fact the only working solution is the one suggested by Sunny. Thank's a lot. Regards, – MoCapitan May 07 '13 at 09:36

0 Answers0