AutoMock Abstract Class

2 posts, 0 answers
  1. Nick
    Nick avatar
    3 posts
    Member since:
    Jan 2012

    Posted 21 Aug 2014 Link to this post

    Hi, is it possible to use MockingContainer<T> where T is an abstract class, such as the following:

    01.public abstract class SimpleBaseClass
    02.{
    03.    private readonly IService service;
    04.    protected SimpleBaseClass(IService service)
    05.    {
    06.        this.service = service;
    07.    }
    08.    public void CallServiceMethod()
    09.    {
    10.        this.service.DoLotsOfThings();
    11.    }
    12.}

    I've tried the following, the latter works as expected. 
    01.[TestMethod]
    02.public void AutoMock_AbstractClass_CallsMethod()
    03.{
    04.    var container = new MockingContainer<SimpleBaseClass>();
    05.    //container.Bind<SimpleBaseClass>().ToMock(); // Still fails :(
    06.    container.Arrange<IService>(s => s.DoLotsOfThings()).MustBeCalled();
    07. 
    08.    container.Instance.CallServiceMethod();
    09. 
    10.    container.AssertAll();
    11.}
    12. 
    13.[TestMethod]
    14.public void CreateMock_AbstractClass_CallsMethod()
    15.{
    16.    var service = Mock.Create<IService>();
    17.    var target = Mock.Create<SimpleBaseClass>(Behavior.CallOriginal, service);
    18. 
    19.    target.CallServiceMethod();
    20. 
    21.    service.Assert(s => s.DoLotsOfThings());
    22.}

    I've tried configuring with AutoMockSettings using CallOriginal, but it just seems like NInject always jumps in and fails to find a suitable constructor.
  2. Tsvetomir
    Admin
    Tsvetomir avatar
    17 posts

    Posted 22 Aug 2014 Link to this post

    Hello Nick,

    Yes, this is known limitation of JustMock. I've added this as a feature request in our feedback portal, so you can vote for it. As a workaround I would suggest in the test to call the implementation of the abstract class with public constructor in it.

    I hope this helps.

    Kind Regards,
    Tsvetomir
    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