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 am wondering what the life spane of a dynamic mock is.

I have a dynamic mock that I create in [TestFixtureSetup] like this:

//create a dynamic mock for a ConnectionProvider
this._providerMock = MockManager.MockObject(typeof(ConnectionProvider));
//make sure that we intercept any calls to our mock provider
this._providerMock.ExpectAlways("Initialize");
this._providerMock.ExpectGetAlways("Name", MOCK_PROVIDER_NAME);
this._providerMock.ExpectGetAlways("ConnectionString", CONNECTION_STRING);

because I need to use it in all of the tests, but when I go to access the ConnectionString property I always get an unexpected call error.

Thanks for any help.
asked by jnapier (9.6k points)

1 Answer

0 votes
I am wondering what the life spane of a dynamic mock is.

The lifespan of any mocked type is until you do one of the following:
mock.Verify()
mock.Clear()
MockManager.Verify() -> This verifies ALL mocks
MockManager.Init() -> This clears all mocks

If you create a mock in the TestFixtureSetup, make sure that this mock is Verified in the TestFixtureTearDown. All other mocks should be implicitly verified. The rule is to MockManager.Verify in the same scope as the MockManager.Init.

Remember that if you don't clear or verify the mock it will still be enabled for the next tests that you are doing (This might have a bad effect on your tests if you don't expect the types to be mocked).
Also remember that MockManager.Init() clears all the mocks.

So in your case I would do the following:
1. MockManager.Init() in the [TestFixtureSetup]
2. Create the ConnectionProvider mock in [TestFixtureSetup]
3. (optional) Create other mocks in the [Setup] and [Test]
4. (optional) Verify other mocks in [TearDown] and [Test]
5. MockManager.Verify() in the [TestFixtureTearDown]
answered by richard (3.9k points)
...