chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes

It seems that isolated attribute clears the fakes when exiting the test method.
I think it should clear the fakes at the end of the teardown.
the following example demonstrate such a failure:
    public class IsolatedBeforeTearDown
        static UnderTest target;

        public void Setup()
            target = new UnderTest();
            Isolate.WhenCalled(() => target.ReturnFive()).WillReturn(6);

        public void TearDown()
            Assert.AreEqual(6, target.ReturnFive());

        public void bTest()
            Assert.AreEqual(6, target.ReturnFive());


And while we are on it, it appears that the Isolated attribute can decorate other methods and not only test methods.
I don't think that was what the intended meaning of the attribute was
(We just spend two hours worth of debugging just because someone put it on top of the setup method...)
I think the two things related, and if you can cause the fakes to be clean at the end of the test (and not the test method) it would solve both issues.
asked by error (6.6k points)

4 Answers

0 votes
Hi Lior, sorry for the late reply,

You're right, currently [Isolated] will clean up all expectations after it exits the test method.
I think what you're suggesting is a good idea - if there exists is a teardown attribute/method, delay the cleanup until the end of the teardown.

As a workaround, perhaps you could skip the [Isolated] attribute in this case, and manually call MockManager.ClearAll() at the end of teardown.
answered by igal (5.7k points)
0 votes

Actually [Isolated] will work on every method and not just test methods.
Also, tried using MockManager as a fix, and that one didn't work.

Two other things did work:
1) Moving the isolated attribute from the class level to the TearDown method.
2) manually calling Isolate.CleanUp at the end of the TearDown .

Both however are really awkward, since most developers here are used to putting the Isolated attribute on the class level.
answered by error (6.6k points)
0 votes
Hi igal,

Is there a difference between Isolate.CleanUp() and MockManager.ClearAll()?
answered by Neil (27.7k points)
0 votes
There is no difference between Isolate.CleanUp() and MockManager.ClearAll(). In fact, Isolate.CleanUp calls ClearAll internally. When working on the Isolate API, we've decided we'll need to distance it from the old one.
answered by yoel (1.9k points)