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
Hi,

any idea why this throws an exception?
       public class BaseClass
        {
            
            public BaseClass()
            {
                throw new NotImplementedException();
            }
        }
        public class MyClass : BaseClass
        {
            
            public MyClass()
            {
                //access database or something costly
            }
            
       }

      [Test]
        public void IsolatingBaseCtor()
        {
            MyClass fake = Isolate.Fake.Instance<MyClass>(Members.CallOriginal, 
                ConstructorWillBe.Called, 
                ConstructorWillBe.Ignored);
        }
Exception:
TestCase 'IsolatorBasics.Examples.IsolatingBaseCtor'
failed: TypeMock.TypeMockException : 
*** Constructor with specified arguments was not found in type IsolatorBasics.Examples+MyClass
  ----> System.MissingMethodException : Constructor on type 'IsolatorBasics.Examples+MyClass' not found.
   at er.CreateFakeInstance[T](Members behavior, Constructor constructorFlag, Constructor baseConstructorFlag, Type baseType, Object[] ctorArgs)
   at er.Instance[T](Members behavior, ConstructorWillBe constructorBehavior, Object[] constructorArguments)
   C:\Users\Lior\Documents\Agile Courses\TDD.Net\Day 2\Second Half Day\Lab\Isolator Basics\IsolatorBasics\Examples.cs(228,0): at IsolatorBasics.Examples.IsolatingBaseCtor()
   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:\Users\Lior\Documents\Agile Courses\TDD.Net\Day 2\Second Half Day\Lab\Isolator Basics\IsolatorBasics\Examples.cs(227,0): at IsolatorBasics.Examples.IsolatingBaseCtor()
   --MissingMethodException
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at TypeMock.MockManager.a(Type A_0, Object[] A_1)
   at TypeMock.MockManager.b(Type A_0, Object[] A_1)
   at TypeMock.MockManager.MockObject(Type type, Constructor mockConstructors, Object[] args)
   at TypeMock.MockManager.MockObject[TMockedType](Constructor mockConstructors, Object[] args)
   at TypeMock.ArrangeActAssert.InstanceFakerBase`1.d()

asked by error (6.6k points)

3 Answers

0 votes
Hi Lior,

The error occurs since the 3rd parameter is ConstructorWillBe.Ignored instead of BaseConstructorWillBe.Ignored.

It may be a good idea to give a better message in this specific case, I'll add it to our backlog.

Best Regards,
Elisha
Typemock Support Team
answered by Elisha (12k points)
0 votes
Hi Elisha,
thank you for the tip.
:idea: it might be a good idea to prevent this from compiling instead of giving an error message during runtime.

also I've changed the test and i now have the following:
public void IsolatingBaseCtor()
{
    MyClass fake = Isolate.Fake.Instance<MyClass>(Members.CallOriginal, 
        ConstructorWillBe.Called, 
        BaseConstructorWillBe.Ignored);
    Isolate.Swap.NextInstance<MyClass>().With(fake);


    MyClass VarName = new MyClass();
}


now for some reason i reach the base class ctor resulting in a an exception being thrown. any idea what might be causing this?

Ive also tried using Members.MustSpecifyReturnValues, and than i got an TypemockUnexpectedCall exception:
failed: TypeMock.TypeMockException :
*** Unexpected Call to IsolatorBasics.Examples+MyClass..ctor()
this kind of feel strange to me. since I've just specified how to handle the ctor.
answered by error (6.6k points)
0 votes
Hi Lior,

This is a bug, Isolator didn't swap the instance with the fake you defined and used the real type. It happens in the scenarios you mentioned, but it does swap with recursive fake.

Thanks for letting us know, we'll fix it in future version.

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