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
I have many tests which already ran with 5.3.1. Now I installed 5.4.5 but Typemock is behaving a bit strange.

When I start
TMockRunner.exe nunit.exe MyCompany.Portal.Web.UnitTest.nunit
and I run all tests, then several tests fail. When I click Run again without changing anything, then different tests fail. On all successive runs the same tests fail as at the 2nd try.

Here is one example of a test which fails only at the 2nd and successive runs:
        [Test, Category(TestCategory.StandardUnitTest)]
        public void ShouldCreateValidJavascript()
        {
            PlaceHolder ph = new PlaceHolder();
            DropDownList ddl = new DropDownList();
            TextBox tb = new TextBox();
            tb.ID = "IPCountryTB";
            TextBox tbox = new TextBox();
            tbox.ID = "IPRegionTB";
            
            ph.Controls.Add(ddl);
            ph.Controls.Add(tb);
            ph.Controls.Add(tbox);

            Isolate.WhenCalled(() => PageObject.GetControl(ph, "IPCountryTB")).WithExactArguments().WillReturn(tb);
            Isolate.WhenCalled(() => PageObject.GetControl(ph, "IPRegionTB")).WithExactArguments().WillReturn(tbox);
            ContainsMultiWarning warningValidator = new ContainsMultiWarning(new object[] { "ContainsMultiWarning", "tooltip", "warning", "invalid", "IPCountryTB,IT", "IPRegionTB,AT" }, ph);

            Assert.AreEqual(warningValidator.ToJavascript(), "new ContainsMultiWarning("tooltip", "warning", "invalid", "IPCountryTB", "IT", "IPRegionTB", "AT")");
        }


When I run it, an exception is thrown at the first usage of Isolate - the first Isolate.WhenCalled. GetControl is a recursive public static method of PageObject.

Here is the exception:
MyCompany.Portal.Web.UnitTest.Components.RegistrationValidators.ContainsMultiWarningTest.ShouldCreateValidJavascript:
System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
----> System.NullReferenceException : Object reference not set to an instance of an object.

at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at ic.a(Boolean A_0)
at eu.c(Boolean A_0)
at eu.c(Object A_0)
at TypeMock.ArrangeActAssert.ExpectationEngine`1.a(TResult A_0)
at MyCompany.Portal.Web.UnitTest.Components.RegistrationValidators.ContainsMultiWarningTest.ShouldCreateValidJavascript() in D:devsvn runkmycompanyPortalUnitTestMyCompany.Portal.Web.UnitTestComponentsRegistrationValidatorsContainsMultiWarningTest.cs:line 96
--NullReferenceException
at aq.a()
at bz.a(Type A_0, Object A_1, MethodBase A_2, String A_3, TypeParams A_4)
at bz.b(Object A_0, String A_1, String A_2, MethodBase A_3, Object[] A_4, Object A_5)
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected, Object p1, Object p2)
at MyCompany.Portal.Web.Components.PageObject.GetControl(Control ctrl, String name) in D:devsvn runkmycompanyPortalWebComponentsRegistrationValidators.cs:line 452
at MyCompany.Portal.Web.UnitTest.Components.RegistrationValidators.ContainsMultiWarningTest.<>c__DisplayClass2.<ShouldCreateValidJavascript>b__0() in D:devsvn runkmycompanyPortalUnitTestMyCompany.Portal.Web.UnitTestComponentsRegistrationValidatorsContainsMultiWarningTest.cs:line 96


Can somebody help?
asked by Bussibaer (680 points)

4 Answers

0 votes
I'd like to further investigate this issue - do you have a test project that shows the problem?
answered by dhelper (11.9k points)
0 votes
Sorry, I needed some time to track the problem down and create the test project. It looks like a problem with MockManager.Init and Isolate.WhenCalled. I'll send you the project via email but here is the code of my test:
   class TestThisTest
   {
      [SetUp]
      public virtual void Init()
      {
         MockManager.Init();
      }

      [Test, Category("StandardUnitTest")]
      public void ShouldReturn8()
      {
         Isolate.WhenCalled(() => Helpers.FindSomething(5)).WithExactArguments().WillReturn(8);
         TestThis testObject = new TestThis();
         string actual = testObject.Foo(5);
         Assert.AreEqual("8", actual);
      }

      [Test, Category("StandardUnitTest")]
      public void ShouldReturn12()
      {
         TestThis testObject = new TestThis();
         string actual = testObject.Foo(10);
         Assert.AreEqual("12", actual);
      }
   }


In our live environment all our tests inherit from a TestBase which includes the setup method with MockManager.Init(). I added it here to the test class for convenience.

When I open the nunit console with
TMockRunner.exe nunit.exe TypemockTest.nunit
and run all tests, then all tests pass. When I click Run a second time, then ShouldReturn8 fails with the exception I posted already.

If I remove the call to MockManager.Init, then it works.
If I remove the call to Isolate.WhenCalled, then it works too - in the test project. In my business project I need to mock this call.

We're using nunit 2.5.2.

Thanks for your help!
answered by Bussibaer (680 points)
0 votes
Just to let you know, I solved the problems.

The first problem which I explained in this post in detail was solved by adding the TypeMock.ArrangeActAssert.IsolatedAttribute to the tests which ran fine the first time, but failed on all successive runs. Typemock support helped me with this one.

But unfortunately we had many other tests which also failed after the update to Typemock 5.4.5 (the same with 6.0). The strange thing was, that they only failed, when I ran all unittests of my project at once. When I started the tests of only one class, then they got green. After some investigation I recognized that the tests in question all had the TypeMock.VerifyMocksAttribute. When I removed that attribute, all tests were green again.

So now all my tests are green again. Although it troubles me a bit that I couldn't find a description of those attributes and therefore a reason why the tests failed before and work now. Maybe the tests needed the VerifyMocks attribute to test everything?

I'm afraid we have to rewrite the unittests. Most of them use a framework to mock the ASP.NET HttpContext, Session, ... This framework was written by somebody who already left the company and it uses the old syntax of natural and reflective mocks. I don't really understand what it all does, so it's not easy for me to find bugs there. So maybe it's better to rewrite it anyway.
answered by Bussibaer (680 points)
0 votes
Hi,

As to [VerifyMocks] it's an attribute from previous generation of API used to implicitly verify calls on mocks.

Moving to the new API is very helpful, it's designed to improve the readability and ease the maintainability of the tests. If you have questions while converting the tests you're welcome to post them.

Regards,
Elisha,
Typemock Support
answered by Elisha (12k points)
...