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

Bug: Nested AutoMock Arrangements are not called

1 Answer 24 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Nicholas
Top achievements
Rank 1
Nicholas asked on 17 Jul 2014, 09:29 PM
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();
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 18 Jul 2014, 08:00 AM
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.

 
Tags
General Discussions
Asked by
Nicholas
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or