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

Can I get Mock.Assert to work in TestCleanup

1 Answer 33 Views
JustMock Free Edition
This is a migrated thread and some comments may be shown as answers.
Andre
Top achievements
Rank 1
Andre asked on 07 Jul 2014, 11:37 PM
I'm using Visual Studio 2013 MSTest
JustMock Lite 2014.2.609.3 (via NuGet)

I'm trying to move the "MustBeCalled" expectations to TestCleanup.
Here is an example of code.

01.[TestClass]
02.public class TestFixture
03.{
04.  private ICloneable _svc;
05. 
06.  [TestInitialize]
07.  public void TestSetup() { _svc = Mock.Create<ICloneable>(); }
08. 
09.  [TestCleanup]
10.  public void TestCleanup() { Mock.Assert(_svc); }
11. 
12.  [TestMethod]
13.  public void TryStuff()
14.  {
15.    // arrange
16.    _svc = Mock.Create<ICloneable>();
17.    Mock.Arrange(()=> _svc.Clone()).MustBeCalled();
18. 
19.    // act
20.    //_svc.Clone();
21. 
22.    // assert
23.  }
24.}

If I run the code as is, the test will pass (regardless if line 20 is commented or not).

This can be fixed by moving the Mock.Assert to line 22+ but ideally I would like to "auto call" Mock.Assert at the end of every test.  I thought that adding it to TestCleanup would work but it doesn't appear to do so.

It appears that prior to TestCleanup running, JustMock will clear out the expectations prior to running TestCleanup.
Is there anyway around this?

Why am I doing this?
Because I'd actually like to write tests like this.

01.public class TestFixture
02.{
03.  private List<object> _assertList = new List<object>();
04. 
05.  [TestCleanup]
06.  public void TestCleanup() { foreach (var svc in _assertList) { Mock.Assert(svc); } }
07.   
08.  private void CloneableMustBeCalled()
09.  {
10.    Mock.Arrange(() => _svc.Clone()).MustBeCalled();
11.    if (!_assertList.Contains(_svc)) { _assertList.Add(_svc); }
12.  }
13.}

Of course my actual test classes will be more complex. But this approach allows me to Mock out MustBeCalled expectations in a more readable manner.  Currently, the test author will have to remember to call Assert the _svc class (or _assertList) at the end of every TestMethod, but was hoping for a more maintainable solution.

Thanks

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 10 Jul 2014, 03:32 PM
Hi Andre,

You cannot assert expectations in TestCleanup that were made in TestMethod's. You can only assert expectations set in TestInitialize, ClassInitialize or AssemblyInitialize.

You can get shorter tests by asserting MustBeCalled expectations explicitly at the end of each test, instead of specifying the expectation in the arrangement.

So, instead of writing
this._svc = Mock.Create<ICloneable>();
this.CloneableMustBeCalled();
this._svc.Clone();
you would write
var svc = Mock.Create<ICloneable>();
svc.Clone();
Mock.Assert(() => svc.Clone());

Both ways are of comparable length and the second way has the added benefit that all the expectations are written in one place, instead of being hidden behind helper methods. I believe this approach would ultimately prove to be more maintainable as a whole.

I hope this helps.

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
JustMock Free Edition
Asked by
Andre
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or