Can I get Mock.Assert to work in TestCleanup

2 posts, 0 answers
  1. Andre
    Andre avatar
    4 posts
    Member since:
    Jun 2011

    Posted 07 Jul 2014 Link to this post

    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.

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

    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
    03.  private List<object> _assertList = new List<object>();
    05.  [TestCleanup]
    06.  public void TestCleanup() { foreach (var svc in _assertList) { Mock.Assert(svc); } }
    08.  private void CloneableMustBeCalled()
    09.  {
    10.    Mock.Arrange(() => _svc.Clone()).MustBeCalled();
    11.    if (!_assertList.Contains(_svc)) { _assertList.Add(_svc); }
    12.  }

    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.

  2. Stefan
    Stefan avatar
    198 posts

    Posted 10 Jul 2014 Link to this post

    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>();
    you would write
    var svc = Mock.Create<ICloneable>();
    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.


    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.

Back to Top