Bug recursively mocking IEnumerable<T>

2 posts, 0 answers
  1. Micah
    Micah avatar
    6 posts
    Member since:
    Sep 2013

    Posted 30 Jan 2014 Link to this post

    01.using System;
    02.using System.Collections.Generic;
    03.using Microsoft.VisualStudio.TestTools.UnitTesting;
    04.using Telerik.JustMock;
    06.namespace JustMockTestProject
    08.    public interface IFoo
    09.    {
    10.        IBar Bar { get; }
    11.    }
    13.    public interface IBar : IEnumerable<Object>
    14.    {
    15.        IBaz GetBaz();
    16.    }
    18.    public interface IBaz {}
    20.    [TestClass]
    21.    public class JustMockTest
    22.    {
    23.        [TestMethod]
    24.        public void TestMethod1()
    25.        {
    26.            var foo = Mock.Create<IFoo>(Behavior.RecursiveLoose);
    27.            var bar = Mock.Create<IBar>(Behavior.RecursiveLoose);
    29.            Assert.IsNotNull(bar.GetBaz()); // passes
    30.            Assert.IsNotNull(foo.Bar.GetBaz()); // fails
    31.        }
    32.    }

    Expected Behavior: RecursiveLoose mocking behavior will cause every method to return a mocked object for reference types.
    Actual Behavior: When RecursiveLoose runs into an interface that derives from IEnumerable<T>, the returned mocked object does not get recursively mocked causing NullReferenceException when a test attempts to access members on the mocked object (IBaz in the example above).

    The above is a simplified test case to narrow down the source of the bug.  If I have an IEnumerable<T> property on IFoo instead, then the returned object is correctly mocked (GetEnumerator doesn't return null).

    As shown in the above example on line 29, mocking an IBar directly works.  It is only when attempting to recursively mock an object that derives from IEnumerable that the problem occurs.
  2. Kaloyan
    Kaloyan avatar
    874 posts

    Posted 04 Feb 2014 Link to this post

    Hello Micah,

    Thank you for pointing this out. It is clearly a bug in our product. I am adding it into the backlog and it should be fixed in the near future releases.

    I have also updated your Telerik points.

    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top