NullReferenceException when mocking property getter

2 posts, 0 answers
  1. Steve
    Steve avatar
    1 posts
    Member since:
    Jan 2011

    Posted 28 Mar 2011 Link to this post

    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
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 29 Mar 2011 Link to this post

    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
  3. DevCraft R3 2016 release webinar banner
Back to Top