Bug: Nested AutoMock Arrangements are not called

2 posts, 0 answers
  1. Nicholas
    Nicholas avatar
    1 posts
    Member since:
    Jul 2014

    Posted 17 Jul 2014 Link to this post

    The following works:
    using NUnit.Framework;
    using Telerik.JustMock.AutoMock;
     
    namespace AutoMockBug
    {
        public interface IBar
        {
            void DoNothing();
        }
     
        public class Baz
        {
            public IBar Bar;
            public Baz(IBar bar)
            {
                Bar = bar;
            }
        }
     
        [TestFixture]
        public class Class1
        {
            [Test]
            public void test()
            {
                var container = new MockingContainer<Baz>();
                container.Arrange<IBar>(x => x.DoNothing()).OccursOnce();
     
                container.Instance.Bar.DoNothing();
     
                container.Assert();
            }
        }
    }

    This doesn't work:
    using NUnit.Framework;
    using Telerik.JustMock.AutoMock;
     
    namespace AutoMockBug
    {
        public interface IFoo
        {
            IBar GetBar();
        }
     
        public interface IBar
        {
            void DoNothing();
        }
     
        public class Baz
        {
            public IFoo Foo;
            public Baz(IFoo foo)
            {
                Foo = foo;
            }
        }
     
        [TestFixture]
        public class Class1
        {
            [Test]
            public void test()
            {
                var container = new MockingContainer<Baz>();
                container.Arrange<IBar>(x => x.DoNothing()).OccursOnce();
     
                container.Instance.Foo.GetBar().DoNothing();
     
                container.Assert();
            }
        }
    }
  2. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 18 Jul 2014 Link to this post

    Hello Nicholas,

    In the second listing, the instance returned by GetBar() is not created and managed by the container, but by the recursive mocking behavior of regular mocks. Only instances created as part of dependency injection (constructor argument injection or property injection) are managed by the container and can be arranged directly from the container.

    The correct arrangement in the second listing would be:
    container.Arrange<IFoo>(x => x.GetBar().DoNothing()).OccursOnce();
    as IFoo is a dependency injected into Baz and hence is managed by the container.

    Regards,
    Stefan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. DevCraft R3 2016 release webinar banner
Back to Top