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
I've used TypeMock to mock an API of ours which is still in development / work in progress.

I have a test assembly (which uses TypeMock) of similarly named classes which you instantiate, and call a method on (to being mocking of a particular namespace), then any code which targets the real API gets my mocked versions.

So far, so straightforward.

I have a question about MockAll, in terms of lifetime/scoping.

We have a GUI (which is also work in progress) that needs to be able to use either the real, or mock API, and to be able to switch between them at runtime for testing. This will continue during the development process.

I wasn't sure if we should modify the GUI so that it read a setting from a Config file (or something like that), and if mocking was specified in the setting it instaantiated my mock assemby and called the mocking begin method (and kept a reference alive to my mock assembly), or whether we would be better leaving the GUI unchanged and instead provide a small command line EXE which instantiates the mocked assembly/enables mocking and which the developer can choose to start or not before they begin testing the UI?

From a lifetime point of view, when you call MockAll (from a particular program thread), how long does mocking of that class stay in force? until the user restarts Windows? until the assembly which contains the mocked methods goes out of scope/destroyed when the parent process is destroyed? some other event?

My mock assembly implements IDisposable, and in the Dispose method it clears all mocks. The way I envisaged the assembly being used was that the GUI would instantiate my mocked assembly, and then keep a member variable alive, and finally call dispose to switch mocking off.

If somebofy could clarify these issues that would be most helpful.

-Alastair
asked by alastair.cameron@kal (3.4k points)

1 Answer

0 votes
Hi,
It is quite simple,
The Mocks are alive until one of the following.
1. Verify is called
2. ClearAll is called
3. Init is called
4. When using [VerifyMock] or [ClearMocks] the equivalent method is called at the end of the test, so the Mocks are alive only in the test
5. When using Scope - when the scope is Disposed.

So there should be no problem to run a suite of tests that sometimes mocks the objects and sometime does not.

:arrow: Just be careful not to mock an instance in one test and then use the same instance in another test after the mock is out of scope - This can happen if you set a singleton to be mocked and didn't reset it before other tests.

You can use TypeMock's ObjectState to help you clear these even in very wierd scenarios.

In any case the Tracer will help you find those cases easily.
answered by scott (32k points)
...