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 found that if one of the arguments of a constructor throws an exception, then the Mock simply can't work. Here's my class code
   public class FreeForm
   {
      private int sim=0;
      private PreFreeForm pff;
      public FreeForm(PreFreeForm pffmy)
      {
         pff = pffmy;
      }
      public FreeForm(int smte)
      {
   //      pff = pffMy;
         sim = smte;
         pff.Count=10;
      }
   }



   internal class PreFreeForm
   {
      private int count =0;
      public int Count
      {
         get { return count; }
         set { count = value; }
      }
      public PreFreeForm(int cc)
      {
         count = cc;
      }
   }


   public class SimpleClass
   {
      private FreeForm myff;
      private PreFreeForm prff;
       public static string mName = "TypeMock";
      public string ANumber
      {
         get{return mName;}
      }
      public SimpleClass()
      {
   //      astring.IndexOf("Aline");
         myff = new FreeForm(prff.Count);

      }
   }


Here is the test code
      [Test, VerifyMocks]
      public void MockInternal()
      {
         Mock mo = MockManager.MockAll(typeof(SimpleClass));
         SimpleClass sc = new SimpleClass();
      }


This is the exception I got:
------ Test started: Assembly: ClassLibrary1.dll ------

TestCase 'ClassLibrary1.Class1.MockInternal'
failed: System.NullReferenceException : Object reference not set to an instance of an object.
c:documents and settingssoon hui.esteemsoftmy documentsjobs hird party components ypemockaseclasssimpleclass.cs(17,0): at BaseClass.SimpleClass..ctor()
c:documents and settingssoon hui.esteemsoftmy documentsjobs hird party components ypemockclasslibrary1 estclass.cs(162,0): at ClassLibrary1.Class1.MockInternal()
at TypeMock.MethodDecorator.CallRealMethod()
at TypeMock.DecoratorAttribute.CallDecoratedMethod()
at TypeMock.VerifyMocksAttribute.Execute()
at TypeMock.DecoratorAttribute.CallDecoratedMethod()
at TypeMock.ClearMocksAttribute.Execute()
at TypeMock.MethodDecorator.d()
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)
c:documents and settingssoon hui.esteemsoftmy documentsjobs hird party components ypemockclasslibrary1 estclass.cs(158,0): at ClassLibrary1.Class1.MockInternal()


0 passed, 1 failed, 0 skipped, took 1.17 seconds.

Since I want to mock away the constructor for FreeForm, it shouldn't really matter what I pass into the constructor, even though one of the argument of it will return an exception when called, right?:)

If this is not a bug, you may want to suggest a workaround method so that I can mock the above successfully.
________
A100
asked by nsoonhui (59.1k points)

11 Answers

0 votes
Hi,

Anyway, I'm not really sure I understand what you're actual intention. for me it looks as if I'm looking at half the implementation. you are trying to test SimpleClass which is dependent on TWO other type:
* FreeForm which is created in the constructor
* PreFreeForm which is the prff field (does does not get initialized anywhere).

If everything was in place our actual test should have looked like:
[Test, VerifyMocks]
public void MockInternal()
{
    Mock moprff = MockManager.MockAll(typeof(PreFreeForm));
    moprff.ExpectGetAlways("Count", 7);
    Mock mo = MockManager.MockAll(typeof(FreeForm));
    SimpleClass sc = new SimpleClass();

}


However for this to work the instance field prff MUST be initialized somewhere. If its not done, the CLR will throw a NullReferanceException the minute it encounters the prff.Count statement no matter what is mocked.

:!: in our next release we will include an API for Mocking Fields that as far as i can tell will be extremely useful in this situation. if you like i will add you to our beta users list.

Great! Do add me to your beta user list.

And thanks for the alternative test. :o
________
buy vapir vaporizer
answered by nsoonhui (59.1k points)
...