Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

Mocks work when run in single test, but not in test list

+1 vote
Sorry for the bad subject

I have a test that uses TypeMock to intercept an SMS component. This works fine if I select to run only the one test. When I select two or more tests to run in the same test run, the test fails. The test in question is the ShouldAbortWithSMSNotificationIfReferredNumberAlreadyUsedInCurrentPromo test below.

I have tried to remove the transaction stuff, and I have tried to Sleep the test at various places to see if there is a lag in there somewhere, but with no luck. I would really appreciate any help to get this test to run fine alongside other tests.

Thanks,

Morten

      [TestMethod]
      public void ShouldAbortWithSMSNotificationIfReferredNumberAlreadyRegistered()
      {
         ReturnCodes expected = ReturnCodes.PhoneNumberAlreadyExists;
         MockManager.Init();
         Mock smsMock = MockManager.Mock(typeof(HSLSMSProvider), Constructor.NotMocked);
         smsMock.ExpectAndReturn("EnqueueMessage", ReturnCodes.OK, null).Args("+4741******-2", "The number +4741****** is already registered with ****** and is not eligible for the free €5.", "447797******", SMS.Providers.SMSSendPriority.Normal);

         FreeMoneySMSPromo promo = getPromo();
         ReturnCodes actual = promo.Process("PROMO 1 +4741******", "+4741******-2");

         Assert.AreEqual(expected, actual, "Result didn't match");
         MockManager.Verify();         
      }

      [TestMethod]
      public void ShouldAbortWithSMSNotificationIfReferredNumberAlreadyUsedInCurrentPromo()
      {
         ReturnCodes expected, actual;
         string referringMobileNumber = "+4741******";
         string referredMobileNumber = "+123456789";

         FreeMoneySMSPromo promo = getPromo();

         MockManager.Init();
         Mock smsMock = MockManager.Mock(typeof(HSLSMSProvider), Constructor.Mocked);
         smsMock.ExpectGet("SMSNumber", "4733******");
         smsMock.ExpectAndReturn("EnqueueMessage", ReturnCodes.OK, null).Args(referringMobileNumber, "The number +123456789 is already registered in this promotion and has already got the free €5.", "4733******", SMS.Providers.SMSSendPriority.Normal);

         using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
         {
            PromoDSTableAdapters.PromoPendingAccountsTableAdapter adapter = new PromoPendingAccountsTableAdapter();
            adapter.Insert(referredMobileNumber, (int?)promo.Settings.InitialFreeMoneyValue, 25, false, null);

            expected = ReturnCodes.PhoneNumberAlreadyRegisteredInCurrentPromo;
            actual = promo.Process(string.Format("PROMO 1 {0}", referredMobileNumber), referringMobileNumber);
         }

         Assert.AreEqual(expected, actual, "Result didn't match");
         MockManager.Verify();
      }
asked May 25, 2007 by petteroe (790 points)

2 Answers

0 votes
Hi,
It is hard to find out what is happening. Try using the Tracer to see what instances are mocked and which ones are begin called.

My gut feeling is that HSLSMSProvider is created once (like a singleton) so the first test works (because Mock catches the next 'new') , but the second fails (as it uses the old instance).
To solve this use MockAll.
answered May 25, 2007 by scott (29,080 points)
+1 vote
My gut feeling is that HSLSMSProvider is created once (like a singleton) so the first test works (because Mock catches the next 'new') , but the second fails (as it uses the old instance).
To solve this use MockAll.


Thanks, that did the trick :D

I use a caching of the HSLSMSProvider instance, so you were absolutely right.

Thanks again for a quick answer.

Morten
answered May 25, 2007 by petteroe (790 points)
...