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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi,

I have something similar to the following in a test:

    [TestFixture]
    [Isolated]
    public class SomeTests 
    {

        [SetUp]
        public void SetUp()
        { 
            Isolate.CleanUp();
            try
            {
                Isolate.Fake.StaticConstructor(typeof (Config));
            }
            catch (TypeMockException e)
            {
               //ignore - the static constructor has probably already been faked 
            }
            Isolate.Fake.StaticMethods(typeof(Config));
            ...some more fakes...
        }



When I remove the line that says 'Isolate.CleanUp' and I run the test along with other tests that also try to fake out the 'Config' class, the test fails.

However, the documentation clearly states that the 'Isolated' attribute and 'Isolate.CleanUp' can be used interchangeably (https://www.typemock.com/Docs/UserGuide/newGuide/html/M_TypeMock_ArrangeActAssert_Isolate_CleanUp.htm). This doesn't work for us; is this a bug, is the documentation incorrect, or are we doing something wrong?

Also, we use both AAA and the classic API in our tests (because we started using the classic API and haven't rewritten all our tests to use AAA.) Some instances of the 'Config' class will have been mocked using MockManager.Mock<Config>(). Does that mean I also need to call MockManager.ClearAll() as part of my setup method?

I think my main question is: what do I need to do to make sure that the mocks are cleared before each test is run? Should I use the 'Isolated' attribute, Isolate.CleanUp, MockManager.ClearAll, MockManager.Init, or a combination of the above?

Thanks,
Nick
asked by zcrar70 (2.9k points)

4 Answers

0 votes
hi Nick,

We'll need to check it, since the Isolated attribute replaces the CleanUp method. We'll get back to you whether this is a bug or not.

As for mixing AAA and Reflective mocks: First, try to separate the tests, meaning, don't use both syntaxes in the same test, as it can create unintended results. Either way, you don't need to call both, since they do the same. The only difference is syntactic.

By the way, I recommend putting CleanUp or ClearAll at the TearDown parts, rather than teh setup.
answered by gilz (14.5k points)
0 votes
Thanks Gil - I put the CleanUp method in the SetUp because I suspected that another test in the suite didn't have one of the Isolated attribute or Isolate.CleanUp in the teardown, so when the first test runs, some fakes might still be in place.

We don't usually mix AAA and reflective mocks in the same test, but thanks for mentioning that, I'll keep a look out for this.

Just to confirm - since we have both AAA and reflective mocks, and you mention that AAA and reflective mocks together can have unintended results, do we need to call both Isolate.CleanUp and MockManager.ClearAll in our SetUp/Teardown methods (or do they do the same thing?)

Thanks,
Nick


P.S. If you're checking for a bug in the Isolated/Isolate.CleanUp substitutability, note that the test we were having problems with was attempting to mock both the static constructor and static methods in a class, and that the class would also have been mocked previously using the Reflective API
answered by zcrar70 (2.9k points)
0 votes
Hi,

Both APIs do the same thing, you need to use one.

Now, there's a couple of things to remember about faking a static constructor.

  • If the static constructor was already called, calling Fake on it, doesn't do anything.
  • If you call Fake.Instance or Fake.StaticMethods before Fake.StaticConstructor, it calls the static constructor first. So make sure you use Fake.StaticConsturctor before any Fake/ real method calls


There are some intricacies in faking the static constructor (basically we delay the call to it, and invoke it ourselves prior to instance or method calls if needed), so make sure to follow the rules.

Thanks,
answered by gilz (14.5k points)
0 votes
Hi Nick,

First off, if the Isolated attribute is applied at the test class level, it applies to each individual test within. You don't need to worry about a single test missing the attribute.

As for the original question, I think the difference in results between runs is because the order of cleaning up is different - When using [Isolated] the system is cleaned up after the test is run, and you used Isolate.CleanUp() at the beginning of the set up, meaning the system is cleaned up prior to running the tests. Is there anything in your tests that may cause the first scenario to be problematic?

Regarding mixing the APIs, I would reiterate Gil's recommendation to separate old and new API tests to two test fixtures. It would also give you an easier time writing and maintaining tests as you won't have to remember which attribute goes on which test.

Regards,
Doron
Typemock Support
answered by doron (17.2k points)
...