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