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 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!
0 votes
In the second stage of AAA(Arrange/Act/Assert), we create the real class's object and call the real method which is supposed to be tested. But I have a class which has heavy dependencies across multiple layers of my software stack, and it is almost impossible to instantiate that class without having several pieces of application running. I cannot directly use 'new' keyword and instantiate my class under test.

for e.g. , I cannot use this kind of statements in my unit test.
ClassA a = new ClassA();

One suggestion was to isolate the real class's constructor. I wanted to know, how can I isolate my real class's constructor? Is there any other way to deal with this problem?
asked by (3.1k points)

3 Answers

0 votes
Hi,

You can use Fake.Dependencies api to do that.
Fake.Dependencies fakes the dependencies of the Ctor with the most parameterss and hands you a real object (Not a fake).

So instead of:
ClassA a = new ClassA();

Use:
ClassA real = Isolate.Fake.Dependencies<ClassA>();

You can find more info about this here.

Is that what you had in mind?
answered by (18k points)
0 votes
I tried what you suggested.
//arrange
Isolate.Fake.StaticConstructor<SuperClassOfClassA>();
ClassA real = Isolate.Fake.Dependencies<ClassA>();
Isolate.NonPublic.WhenCalled(real, "method1").CallOriginal();


//act
Isolate.Invoke.Method(real, "method1", null);

//assert
Isolate.Verify.NonPublic.WasCalled(real, "method1");

But now, the point where I call the NonPublic method "method1" , I'm getting a null reference error.
answered by (3.1k points)
0 votes
Hi,

I'm trying to reproduce it with no luck yet. Is it possible to post the code of the two classes in use?
answered by (12k points)
...