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
Am new to TypeMock and trying to mock an object that's encapsulated in another. i.e Have a Class A and Class B, and Class A has a member variable of Class B, when I try mocking Class B and calling Class B's methods within Class A the test fails. Currently looking at Natural Mocks, any ideas PLEASE!
asked by jagunlabi23 (720 points)

6 Answers

0 votes
Hi
Welcome to TypeMock forum 8)
Can you please post the test code so I can see what's the problem?
answered by ohad (35.4k points)
0 votes
below is the test code
=======================
[Test]
[VerifyMocks]
public void mockDummyUpper()
{
// Start Mocking
using (RecordExpectations recorder = new RecordExpectations())
{
// Lets mock the DummyUpper and DummyMiddle class
recorder.MockAllInstances = true;

//mocks the instantiation of the dummy class
DummyUpper dupper = new DummyUpper();

DummyMiddle dmiddle = new DummyMiddle();


recorder.ExpectAndReturn(dupper.getDummyLowerValue(), 11).RepeatAlways();
recorder.ExpectAndReturn(dupper.dm.doSomeCalc(1), 33);
recorder.ExpectAndReturn(DummyLower.DummyValue(1), 21).RepeatAlways();
recorder.ExpectAndReturn(dmiddle.GetDelta, 12).RepeatAlways();
recorder.ExpectAndReturn(dmiddle.doSomeCalc(1), 22).RepeatAlways();

}
// END OF MOCK SETUP

DummyUpper upper2 = new DummyUpper();

DummyMiddle dmiddle2 = new DummyMiddle();


Assert.AreEqual(upper2.dm.doSomeCalc(1),33);

Assert.AreEqual(dmiddle2.GetDelta,12);

Assert.AreEqual(dmiddle2.doSomeCalc(1), 22);

}
========================================
below is the class definition for DummyUpper and DummyMiddle
===========================================
public class DummyUpper
{
public DummyMiddle dm;

public int ReturnFive()
{
int five = 5;
return five;
}

public int getDummyLowerValue()
{
return DummyLower.DummyValue(10);
}

public DummyUpper()
{
dm = new DummyMiddle();
}
}
=======================================
public class DummyMiddle
{
public int GetDelta
{
get
{
return this.delta;
}

set
{
this.delta = value;
}
}

private int delta;

public int doSomeCalc(int val)
{
return (val*10);
}

public DummyMiddle()
{
this.delta = 100;
}


}
==================================
answered by jagunlabi23 (720 points)
0 votes
hi
I'm still missing the definition to DummyLower class
But i can see a problem here:
You are mocking all calls to getDummyLowerValue
as a result DummyLower.DummyValue() method never
get called. My guess is that it leads to Verify Exception.
Could it be the the cause to the error?

:idea: When you run into this kind of problems use TypeMock Tracer

If I'm wrong please post definition to DummyLower class.
answered by ohad (35.4k points)
0 votes
hi

below is DummyLower class
======================
public class DummyLower
{
public static int DummyValue(int i)
{
return (10 * i);
}

}
========================

thanks.
answered by jagunlabi23 (720 points)
0 votes
Hi
ok the problem is dm is not initialized since the constructor of DummyUpper
is mocked.
Another thing is that TypeMock can not mock fields.
This is the reason for the NullReferenceException you get

The solution is simple
Replace:
recorder.ExpectAndReturn(dupper.dm.doSomeCalc(1), 33);

with:
recorder.ExpectAndReturn(dmiddle.doSomeCalc(1), 33);


Hope it helps.
answered by ohad (35.4k points)
0 votes
I really wanted to confirm if TypeMock can mock fields and you have confirmed this, would just have to change the design of my classes.

cheers.
answered by jagunlabi23 (720 points)
...