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'm trying to put initalization code at FixtureSetup, like so:

[TestFixtureSetUp]
public void FixtureSetUp()
{
MockManager.Init();
//Mock SitePrincipal and SiteIdentity
mockPrincipal = MockManager.MockAll(typeof(SitePrincipal),Constructor.Mocked);
mockIdentity = MockManager.MockAll(typeof(SiteIdentity),Constructor.Mocked);

si = new SiteIdentity(12345);

//SitePrinciple.Identity will always return a mocked SiteIdentity
mockPrincipal.ExpectGetAlways("Identity", si);
//SiteIdentity.UserID will always return UserID = 12345
mockIdentity.ExpectGetAlways("UserID", 12345);
}


Where SitePrincipal is used in all the tests.
Will this cause any side effects?

Thanks
asked by ep_hwong (1.1k points)

1 Answer

0 votes
Hi and welcome,

Since TestFixtureSetup is only called once at the initialization of the test fixture all mocks will be cleared when you call MockManager.Verify. and you will probably fail on the following tests that uses the mocks you created in there.

you have two options to solve this:
1) move everything to [SetUp]
2) you can instruct the mock not to be cleared by using VerifyMode.DontClear as follows:
[TestFixtureSetUp] 
public void FixtureSetUp() 
{ 
  MockManager.Init(); 
  //Mock SitePrincipal and SiteIdentity 
  mockPrincipal = MockManager.MockAll(typeof(SitePrincipal),Constructor.Mocked); 
  mockPrincipal.StartBlock(VerifyMode.DontClear);
  mockIdentity = MockManager.MockAll(typeof(SiteIdentity),Constructor.Mocked); 
  mockIdentityl.StartBlock(VerifyMode.DontClear);
  si = new SiteIdentity(12345); 

  //SitePrinciple.Identity will always return a mocked SiteIdentity 
  mockPrincipal.ExpectGetAlways("Identity", si); 
  //SiteIdentity.UserID will always return UserID = 12345 
  mockIdentity.ExpectGetAlways("UserID", 12345);
  mockIdentity.EndBlock();
  mockPrincipal.EndBlock();
} 


the second option will require an enterprise license. but will enable you to keep the code in the TestFixtureSetup.

one final note, using MockAll will cause ALL instances created for the given class to be mocked.

Hope this helps
answered by lior (13.2k points)
...