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

Asserting calls on AutoMock'ed dependencies

1 Answer 134 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Nick
Top achievements
Rank 1
Nick asked on 17 Jul 2014, 01:14 PM
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

1 Answer, 1 is accepted

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

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