This is a migrated thread and some comments may be shown as answers.

NullReferenceException when mocking property getter

1 Answer 129 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 28 Mar 2011, 07:13 PM
I'm getting a NullReferenceException when I try to Arrange a basic property:

Source:
public DbEntityEntry<TEntityType> Entry<TEntityType>(TEntityType Entity) where TEntityType : class
{
    if (_entries.ContainsKey(Entity))
    {
        return _entries[Entity] as DbEntityEntry<TEntityType>;
    }
    else
    {
        var mockEntry = Mock.Create<DbEntityEntry<TEntityType>>();
        Mock.Arrange<TEntityType>(() => mockEntry.Entity).Returns(Entity as TEntityType);
        _entries.Add(Entity, mockEntry);
        return mockEntry;
    }
}



The NullReferenceException throws on the "Mock.Arrange..." line, with the following stack trace:

   at Telerik.JustMock.DynamicProxy.MethodInvocation.Continue() in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\DynamicProxy\MethodInvocation.cs:line 159
   at Telerik.JustMock.Interceptors.ProxyInterceptor.Intercept(IInvocation invocation) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Interceptors\ProxyInterceptor.cs:line 31
   at DbEntityEntry`1_Proxy_d3528d76a583445191d2089b7dd0b53f._GetHashCode(MethodInvocation , Int32 , Boolean )
   at DbEntityEntry`1_Proxy_d3528d76a583445191d2089b7dd0b53f.GetHashCode()
   at Telerik.JustMock.Weaver.ContainerContext.GetKey(Type targetType, MethodInfo methodInfo, Object target) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Weaver\ContainerContext.cs:line 134
   at Telerik.JustMock.Weaver.ContainerContext.Get(Type targetType, MethodInfo methodInfo, Object target) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Weaver\ContainerContext.cs:line 91
   at Telerik.JustMock.Weaver.ContainerContext.Get(MethodInfo mi, Object target) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Weaver\ContainerContext.cs:line 77
   at Telerik.JustMock.Weaver.Interceptors.WeaverInterceptor.OnInvocation(IInvocation invocation) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Weaver\Interceptors\WeaverInterceptor.cs:line 97
   at Telerik.JustMock.Weaver.Interceptors.WeaverInterceptor.Telerik.JustMock.Weaver.Interceptors.Abstraction.IWeaverInterceptor.Intercept(IInvocation invocation) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Weaver\Interceptors\WeaverInterceptor.cs:line 44
   at Telerik.JustMock.MockContext`1.SetupMock(Object obj, Type mockTarget, MethodInfo method, Expression expression) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\MockContext.cs:line 109
   at Telerik.JustMock.MockContext`1.SetupMock(Expression`1 expression) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\MockContext.cs:line 26
   at Telerik.JustMock.Mock.<>c__DisplayClass1`1.<Arrange>b__0(MockContext`1 x) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Mock.cs:line 74
   at Telerik.JustMock.MockContext.Setup[TDelgate,TReturn](Instruction instruction, Func2`2 function) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\MockContext.cs:line 238
   at Telerik.JustMock.Mock.Arrange[TResult](Expression`1 expression) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Mock.cs:line 72
...


I am using the Free Edition and just updated to the March 16 release, but this didn't help.  Any ideas what I might be doing wrong?

Thanks

1 Answer, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 29 Mar 2011, 11:13 AM
Hi Steve,

Thanks again for making the post. Here i have created a sample test from your example which seems to pass nicely.


[TestMethod]
public void TestMethod1()
{
    var dbEntity = EntityFactory.Entry<Foo>(new Foo());
    Assert.IsNotNull(dbEntity.Entity);
}
 
public class DbEntityEntry<TEntityType> where TEntityType : class
{
    public virtual TEntityType Entity { get; set; }
}
 
public class EntityFactory
{
    public static DbEntityEntry<TEntityType> Entry<TEntityType>(TEntityType Entity) where TEntityType : class
    {
        var mockEntry = Mock.Create<DbEntityEntry<TEntityType>>();
        Mock.Arrange(() => mockEntry.Entity).Returns(Entity as TEntityType);
        return mockEntry;
    }
}
 
public class Foo{ }

Therefore, you might need to check if mockEntry.Entity is marked as virtual since you are using the free edition and there is no profiler to intercept the non-virtual calls.  Of course, it should not be calling the profiling block at all and should throw a proper exception right away, it is something to fix from our end.

In addition, you don't need to pass the extra TEntityType in Mock.Arrange as well.

Finally, I would recommend you to better test the class from outside (from test method) rather having mock codes within the class itself (as a good practice).

Kind Regards,
Ricky
the Telerik team
Tags
General Discussions
Asked by
Steve
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Share this question
or