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
Hi,

We have a large number of NUnit tests that have been written by various people over a period of time.

In terms of Typemock, some of them use MockManager, some use only AAA API and some use a combination of both.

Practically none of the tests are using the [Isolated] attribute, and sometimes some tests will pass on a local machine (when all run as suite in debug mode), but not on a build machine (again, also all run as a suite but in release mode)

Can you outline best practices for when/where to use [Isolated] attribute or any other required attribute. Also, what should we be looking out so we can get better confidence that when our tests run the behavior is the same on a developer machine as it is on a build server
asked by dplante (1.1k points)

3 Answers

0 votes
Hi dplante,

A simple way to ensure the different APIs don't interfere with each other is to separate tests using older APIs and tests using the AAA API into different test classes, and decorating each test class with its relevant attribute. If this is not applicable for some reason, the [Isolated] attribute should work also for older APIs.

Note you can use these attributes at the class level, which means that each test in the test class is automatically cleaned up - this is recommended as it verifies you never forget to cleanup a test.

HTH,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Thanks,

So a couple of questions:
1. Is there any documentations that show how to map usage of MockManager type API to AAA API? (showing what call in one API maps to call in later API?) - this would help upgrade tests that use both APIs simultaneously

2. Is there any difference with putting [Isolated] at class level or method level? Can we put it at assembly level?

3. In our large body of tests, I noticed other developers were using AAA API but we didn't have the [Isolated] attribute anywhere (in any class or method). When I added this to appropriate classes, I started to get error:
"Cannot use WhenCalled without a complementing behavior"

I noticed that the cause of this seemed to be statements like this in tests
Isolate.WhenCalled(() => myInstance.ReturnFive()); // improper use or API - no action like WillThrow(new Exception());

So without [Isolated] attribute anywhere, compilation would work, and tests would not complain with error ""Cannot use WhenCalled without a complementing behavior", so we had two issues:

1. we weren't using [Isolated] attribute
2. we weren't using API properly, which TypeMock would catch when we did start using [Isolated] attribute

So my question is, when we weren't using API properly AND we didn't have Isolated attribute, what is behavior of the API (does it ignore the statements or does it do something else?)

Thanks,
Dominique
answered by dplante (1.1k points)
0 votes
1. There's a conceptual difference between the AAA API and Record/Replay model (reflective mocks), therefore there is no one-to-one mapping between the APIs. I suggest you have a look in our User Guide under Using Typemock Isolator - C# API to learn how to use the AAA API.

2. If you place the [Isolated] attribute at class level, it will be applied to all the methods in the class. If you have some tests that use Isolator, and some that don't in the same class, it's still better to place the [Isolated] attribute at the class level, rather than individual tests.
We don't support placing this attribute in the assembly level, but that's an interesting idea! Thanks for your suggestion.

3. Due to the nature of a fluent API, there's no way currently to ensure that the API was properly called in compile time. This is an issue we're actively looking at. Without the [Isolated] attribute and without a complementing statement, the API does not record an expectation, so it effectively does nothing.
In the mean time, just ensure proper use of the API as well as usage of [Isolated] attribute.
answered by igal (5.7k points)
...