Asserting calls on AutoMock'ed dependencies

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

    Posted 17 Jul 2014 Link to this post

    Am I correct in saying that when using the AutoMocking container to assert a call on a dependency, you *have* to create some form of expectation which is then asserted (e.g. with AssertAll), rather than just defining the assertion at the end?

    For example, the following works:
    [TestMethod]
    public void Test()
    {
     var c = new MockingContainer<Target>();
     c.Arrange<ITargetDependency>(x => x.ShouldBeCalled());

     c.Instance.WorkYourMagic();

     c.AssertAll(); // will fail if WorkYourMagic does not call ITargetDependency.ShouldBeCalled()
    }

    However, this does not...
    [TestMethod]
    public void Test()
    {
     var c = new MockingContainer<Target>();

     c.Instance.WorkYourMagic();

     c.Assert<ITargetDependency>(x => x.ShouldBeCalled()); // Always claims WorkYourMagic does not call ITargetDependency.ShouldBeCalled()
    }

    Regards,
    Nick
  2. Answer
    Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 18 Jul 2014 Link to this post

    Hello Nick,

    You don't have create expectations up-front. All you need to do is tell the container that you intend to test that dependency afterwards. The simplest way to do it is like so:
    [TestMethod]
    public void Test()
    {
     var c = new MockingContainer<Target>();
     c.Bind<ITargetDependency>().ToMock();
     
     c.Instance.WorkYourMagic();
     
     c.Assert<ITargetDependency>(x => x.ShouldBeCalled());
    }
    The ToMock() binding then allows you to do assertions on that instance.

    Granted, this appears to be a needless complication in this extremely simple and straightforward scenario. I'll fix the code so that this scenario will no longer require the ToMock() binding and your second listing will then just work. The fix will be made available in our upcoming internal build due next week.

    As a token of gratitude for uncovering and reporting this issue, I've granted you some Telerik points.

    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