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
Have used below statement for arrange..

Isolate.WhenCalled(() => fakeprimaryM.PrimaryRelationships).WillReturn(primaryRe);


where primaryRe is a primaryrelationship class object. and fakeprimaryM is a fake object for primaryMember.


Even then, when actual application method is gettting called, its throwing below exception.

UDM.Interaction.PrimaryMember.PrimaryRelationships >> Throwing exception for relationships.

Test method TestProject1.UnitTest1.TestActivity threw exception: System.NullReferenceException: Object reference not set to an instance of an object..


Clear()
CXM.BECU.Activity.Activities.Composites.TravelNotifications.TNMapRelationshipInfo.ExecuteSurfaceMethod(ActivityExecutionStatus status) in C:Documents and Settings ajusiMy DocumentsVisual Studio 2008ProjectsPhase2CXM.BECUCXM.BECUCXM.BECU.ActivityActivitiesCompositesTravelNotificationsTNMapRelationshipInfo.cs: line 27
a2.a(Object A_0, MethodInfo A_1, Object[] A_2)
a2.a(Object A_0, String A_1, Object[] A_2)
TestProject1.UnitTest1.TestActivity() in C:Documents and Settings ajusiMy DocumentsVisual Studio 2008ProjectsTestProject1TestProject1UnitTest1.cs: line 113
TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Boolean A_5, Object[] A_6)
TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected, Boolean isInterceptedType)
TestProject1.UnitTest1.TestActivity() in C:Documents and Settings ajusiMy DocumentsVisual Studio 2008ProjectsTestProject1TestProject1UnitTest1.cs: line 0
asked by Sivakumar_Raju (840 points)

10 Answers

0 votes
Welcome Raju,

How do you pass the fake into the code under test.
Is it an argument? or a future object?
answered by scott (32k points)
0 votes
Hi,
fake is a future object. Its not passed as an argument.

UDM.Interaction.PrimaryMember.PrimaryRelationships is the actual code in the project which am trying to access by creating fake objects in our Test project, but when the actual code "UDM.Interaction.PrimaryMember.PrimaryRelationships" is called, it fails for the null reference error shown below, though the fake object is created like >> Isolate.WhenCalled(() => fakeprimaryM.PrimaryRelationships).WillReturn(primaryRe);

Thanks,
Siva
answered by Sivakumar_Raju (840 points)
0 votes
I understand,
I think that the problem is in setting the future object.
Do you have this line?
Isolate.Swap.AllInstances<PrimaryMember>().With(fakeprimaryM);
answered by scott (32k points)
0 votes
Have below statements:

CXM.BECU.DataModel.PrimaryMemberClass fakeprimaryM = Isolate.Fake.Instance<CXM.BECU.DataModel.PrimaryMemberClass>();
Isolate.Swap.NextInstance<CXM.BECU.DataModel.PrimaryMemberClass>().With(fakeprimaryM);

CXM.BECU.DataModel.PrimaryRelationshipClass fakeprimaryR = Isolate.Fake.Instance<CXM.BECU.DataModel.PrimaryRelationshipClass>();
Isolate.Swap.NextInstance<CXM.BECU.DataModel.PrimaryRelationshipClass>().With(fakeprimaryR);


wont this serve the purpose ? You want me to try it with the one you posted ?
answered by Sivakumar_Raju (840 points)
0 votes
Tried with AllInstances too, but same result.. runtime exception for nullreference for primaryrelationship object in actual app code.. i doubt for some reason, the fake objects are not replacing the actual app objects..
answered by Sivakumar_Raju (840 points)
0 votes
What method is failing? can you post the code.

please post complete stack trace.
What is in TNMapRelationshipInfo.cs: line 27
answered by scott (32k points)
0 votes
Complete trace :




'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_32mscorlib.0.0.0__b77a5c561934e089mscorlib.dll'
'vstesthost.exe' (Managed): Loaded 'C:Program FilesMicrosoft Visual Studio 9.0Common7IDEstesthost.exe'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_MSILSystem.Runtime.Remoting.0.0.0__b77a5c561934e089System.Runtime.Remoting.dll'
'vstesthost.exe' (Managed): Loaded 'C:Program FilesMicrosoft Visual Studio 9.0Common7IDEPrivateAssembliesMicrosoft.VisualStudio.QualityTools.Common.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_MSILSystem.0.0.0__b77a5c561934e089System.dll'
'vstesthost.exe' (Managed): Loaded 'C:Program FilesMicrosoft Visual Studio 9.0Common7IDEPrivateAssembliesMicrosoft.VisualStudio.QualityTools.ExecutionCommon.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_MSILTypeMock.2.5.0__3dae460033b8d8e2TypeMock.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_MSILTypemock.Interceptors.2.5.0__3dae460033b8d8e2Typemock.Interceptors.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_MSILMicrosoft.VisualStudio.QualityTools.Resource9.0.0.0__b03f5f7f11d50a3aMicrosoft.VisualStudio.QualityTools.Resource.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_MSILSystem.Configuration.0.0.0__b03f5f7f11d50a3aSystem.Configuration.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_MSILSystem.Xml.0.0.0__b77a5c561934e089System.Xml.dll'
'vstesthost.exe' (Managed): Loaded 'C:Program FilesMicrosoft Visual Studio 9.0Common7IDEPrivateAssembliesMicrosoft.VisualStudio.QualityTools.ControllerObject.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_32System.Data.0.0.0__b77a5c561934e089System.Data.dll'
'vstesthost.exe' (Managed): Loaded 'C:Program FilesMicrosoft Visual Studio 9.0Common7IDEPrivateAssembliesMicrosoft.VisualStudio.QualityTools.AgentObject.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_32System.Web.0.0.0__b03f5f7f11d50a3aSystem.Web.dll'
'vstesthost.exe' (Managed): Loaded 'C:Program FilesMicrosoft Visual Studio 9.0Common7IDEPrivateAssembliesMicrosoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_MSILMicrosoft.VisualStudio.QualityTools.UnitTestFramework9.0.0.0__b03f5f7f11d50a3aMicrosoft.VisualStudio.QualityTools.UnitTestFramework.dll'
The thread 0x818 has exited with code 0 (0x0).
'vstesthost.exe' (Managed): Loaded 'C:Program FilesMicrosoft Visual Studio 9.0Common7IDEPrivateAssembliesMicrosoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter.dll'
'vstesthost.exe' (Managed): Loaded 'C:Program FilesMicrosoft Visual Studio 9.0Common7IDEPrivateAssembliesMicrosoft.VisualStudio.QualityTools.Tips.UnitTest.AssemblyResolver.dll'
'vstesthost.exe' (Managed): Loaded 'C:Documents and Settings ajusiMy DocumentsVisual Studio 2008ProjectsPhase2CXM.BECUTestResults ajusi_WS-39700 2012-03-05 07_46_56OutTestProject1.dll', Symbols loaded.
'vstesthost.exe' (Managed): Loaded 'C:Documents and Settings ajusiMy DocumentsVisual Studio 2008ProjectsPhase2CXM.BECUTestResults ajusi_WS-39700 2012-03-05 07_46_56OutTypemock.ArrangeActAssert.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_MSILConfiguration.2.5.0__3dae460033b8d8e2Configuration.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_32CXM.Rudder.DataModel.0.0.35__89cd864dc3276342CXM.Rudder.DataModel.dll'
'vstesthost.exe' (Managed): Loaded 'C:Documents and Settings ajusiMy DocumentsVisual Studio 2008ProjectsPhase2CXM.BECUTestResults ajusi_WS-39700 2012-03-05 07_46_56OutCXM.BECU.DataModel.dll', Symbols loaded.
'vstesthost.exe' (Managed): Loaded 'C:Documents and Settings ajusiMy DocumentsVisual Studio 2008ProjectsPhase2CXM.BECUTestResults ajusi_WS-39700 2012-03-05 07_46_56OutCXM.BECU.Activity.dll', Symbols loaded.
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_32SOAdesk.SUDS.Rudder.Workflow.Shared.0.0.35__89cd864dc3276342SOAdesk.SUDS.Rudder.Workflow.Shared.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_MSILSystem.Workflow.ComponentModel.0.0.0__31bf3856ad364e35System.Workflow.ComponentModel.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_32SOAdesk.Framework.0.0.35__89cd864dc3276342SOAdesk.Framework.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_32CXM.Rudder.Shared.0.0.35__89cd864dc3276342CXM.Rudder.Shared.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_MSILSystem.Core.5.0.0__b77a5c561934e089System.Core.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_MSILWindowsBase.0.0.0__31bf3856ad364e35WindowsBase.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_MSILSystem.Windows.Forms.0.0.0__b77a5c561934e089System.Windows.Forms.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_MSILSystem.Drawing.0.0.0__b03f5f7f11d50a3aSystem.Drawing.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_MSILMicrosoft.VisualStudio.Debugger.Runtime9.0.0.0__b03f5f7f11d50a3aMicrosoft.VisualStudio.Debugger.Runtime.dll'
'vstesthost.exe' (Managed): Loaded 'C:WINDOWSssemblyGAC_MSILSystem.Workflow.Runtime.0.0.0__31bf3856ad364e35System.Workflow.Runtime.dll'
A first chance exception of type 'System.NullReferenceException' occurred in mscorlib.dll
A first chance exception of type 'System.NullReferenceException' occurred in Typemock.ArrangeActAssert.dll
A first chance exception of type 'System.NullReferenceException' occurred in TypeMock.dll
The thread 0x500 has exited with code 0 (0x0).
'vstesthost.exe' (Managed): Loaded 'pe7s4lwx'
The thread 'Agent: adapter run thread for test 'TestActivity' with id 'caf2300e-0ae7-4d2c-b50d-98e30fd562da'' (0x1bb8) has exited with code 0 (0x0).
The thread 0x11d4 has exited with code 0 (0x0).
The thread 0x1894 has exited with code 0 (0x0).
The thread 0x1dd0 has exited with code 0 (0x0).
The program '[3380] vstesthost.exe: Managed' has exited with code 0 (0x0).
answered by Sivakumar_Raju (840 points)
0 votes
Application code:

namespace CXM.BECU.Activity.Activities.Composites.TravelNotifications
{
public class TNMapRelationshipInfo : CXMActivityBase
{
/// <summary>
/// Please insert all activity code in this method
/// </summary>
protected override System.Workflow.ComponentModel.ActivityExecutionStatus ExecuteSurfaceMethod(System.Workflow.ComponentModel.ActivityExecutionStatus status)
{
//Relationship is populated with sample data for now. Once SFM is ready to get the relationships, this code should be modified accordingly to get from SFM objects.
List<PrimaryRelationshipClass> primaryRelationshipData = new List<PrimaryRelationshipClass>();

UDM.Interaction.PrimaryMember.PrimaryRelationships.Clear(); >>>>>>> This is line 27
answered by Sivakumar_Raju (840 points)
0 votes
The above function is getting called from test Activity project to invoke the appliation code.. as below..


public void TestActivity()
{

CXMActivityBase fakeActivityBase = Isolate.Fake.Instance<CXMActivityBase>(Members.ReturnRecursiveFakes, ConstructorWillBe.Ignored);
Isolate.Swap.NextInstance<CXMActivityBase>().With(fakeActivityBase);

CXM.Rudder.DataModel.RudderDataModel<InteractionClass> fakeUDM = Isolate.Fake.Instance<CXM.Rudder.DataModel.RudderDataModel<InteractionClass>>(Members.ReturnRecursiveFakes, ConstructorWillBe.Ignored);
Isolate.Swap.NextInstance<CXM.Rudder.DataModel.RudderDataModel<InteractionClass>>().With(fakeUDM);

CXM.Rudder.DataModel.InteractionDataModel fakeInteraction = Isolate.Fake.Instance<CXM.Rudder.DataModel.InteractionDataModel>();
Isolate.Swap.NextInstance<CXM.Rudder.DataModel.InteractionDataModel>().With(fakeInteraction);

CXM.BECU.DataModel.InteractionClass fakeInteractionClass = Isolate.Fake.Instance<CXM.BECU.DataModel.InteractionClass>();
Isolate.Swap.NextInstance<CXM.BECU.DataModel.InteractionClass>().With(fakeInteractionClass);

CXM.BECU.DataModel.PrimaryMemberClass fakeprimaryM = Isolate.Fake.Instance<CXM.BECU.DataModel.PrimaryMemberClass>();
Isolate.Swap.NextInstance<CXM.BECU.DataModel.PrimaryMemberClass>().With(fakeprimaryM);

CXM.BECU.DataModel.PrimaryRelationshipClass fakeprimaryR = Isolate.Fake.Instance<CXM.BECU.DataModel.PrimaryRelationshipClass>();
Isolate.Swap.NextInstance<CXM.BECU.DataModel.PrimaryRelationshipClass>().With(fakeprimaryR);
Isolate.Swap.AllInstances<PrimaryMemberClass>().With(fakeprimaryM);
Isolate.Swap.AllInstances<PrimaryRelationshipClass>().With(fakeprimaryR);

CXM.BECU.Activity.Activities.Composites.TravelNotifications.TNMapRelationshipInfo fakeTNMapRelationshipObject = Isolate.Fake.Instance<CXM.BECU.Activity.Activities.Composites.TravelNotifications.TNMapRelationshipInfo>(Members.ReturnRecursiveFakes, ConstructorWillBe.Ignored);
Isolate.NonPublic.WhenCalled(fakeTNMapRelationshipObject, "ExecuteSurfaceMethod").CallOriginal();

Isolate.WhenCalled(() => fakeActivityBase.UDM).WillReturn(fakeUDM);
Isolate.WhenCalled(() => fakeUDM.Interaction).WillReturn(fakeInteractionClass);
Isolate.WhenCalled(() => fakeInteractionClass.PrimaryMember).WillReturn(fakeprimaryM);

CXM.Rudder.DataModel.RudderDataClassCollection<PrimaryRelationshipClass> primaryRe =
new CXM.Rudder.DataModel.RudderDataClassCollection<PrimaryRelationshipClass>(fakeInteractionClass.PrimaryMember, string.Format("PrimaryRelationships"));

PrimaryRelationshipClass primaryRelation =
new PrimaryRelationshipClass(fakeActivityBase.UDM.Interaction.PrimaryMember, string.Format("PrimaryRelationships"));
primaryRe.Add(primaryRelation);

Isolate.WhenCalled(() => fakeprimaryM.PrimaryRelationships).WillReturn(primaryRe);

var result = Isolate.Invoke.Method(fakeTNMapRelationshipObject, "ExecuteSurfaceMethod", System.Workflow.ComponentModel.ActivityExecutionStatus.Executing);

Isolate.Verify.NonPublic.WasCalled(fakeTNMapRelationshipObject, "ExecuteSurfaceMethod").WithArguments(System.Workflow.ComponentModel.ActivityExecutionStatus.Initialized);

}
answered by Sivakumar_Raju (840 points)
0 votes
ok,
first some tips:
:arrow: Members.ReturnRecursiveFakes, ConstructorWillBe.Ignored is the default, no need to be explicit

:arrow: What are you testing? it seems that you first call ExecuteSurfaceMethod and then verify that it was called, which it obviously has

:arrow: It seems that you are faking CXMActivityBase to fake the base of TNMapRelationshipInfo, this doesn't work, actually - there is also no need - once you fake the derived class - you have also faked the base class.

:arrow: I am not 100% sure but this code should work:

// Do you really need this?
var fakeInteraction = Isolate.Fake.Instance<CXM.Rudder.DataModel.InteractionDataModel>();
Isolate.Swap.NextInstance<CXM.Rudder.DataModel.InteractionDataModel>().With(fakeInteraction);

// create class undertest    
var fakeTNMapRelationshipObject = Isolate.Fake.Instance<CXM.BECU.Activity.Activities.Composites.TravelNotifications.TNMapRelationshipInfo>();
Isolate.NonPublic.WhenCalled(fakeTNMapRelationshipObject,"ExecuteSurfaceMethod").CallOriginal();

// Add relationship
var primaryRe = new CXM.Rudder.DataModel.RudderDataClassCollection<PrimaryRelationshipClass>(fakeTNMapRelationshipObject.UDM.Interaction.PrimaryMember, string.Format("PrimaryRelationships"));

PrimaryRelationshipClass primaryRelation =
new PrimaryRelationshipClass(fakeTNMapRelationshipObject.UDM.Interaction.PrimaryMember, string.Format("PrimaryRelationships"));
primaryRe.Add(primaryRelation);

// make sure our code will return the relationship. Note we are chaining the calls - cool!
Isolate.WhenCalled(()=>Isolate.WhenCalled(() =>fakeTNMapRelationshipObject.UDM.Interaction.PrimaryMember.PrimaryRelationships)
    .WillReturn(primaryRe);
  

// Act
var result = Isolate.Invoke.Method(fakeTNMapRelationshipObject, "ExecuteSurfaceMethod", System.Workflow.ComponentModel.ActivityExecutionStatus.Executing);

// Assert?
Isolate.Verify.NonPublic.WasCalled(fakeTNMapRelationshipObject, "ExecuteSurfaceMethod").WithArguments(System.Workflow.ComponentModel.ActivityExecutionStatus.Initialized);


if PrimaryRelationships is settable you can use:
fakeTNMapRelationshipObject.UDM.Interaction.PrimaryMember.PrimaryRelationships = primaryRe


if UDM is not visible use:
// grab UDM, will be fake as fakeTNMapRelationshipObject is fake
CXM.Rudder.DataModel.RudderDataModel<InteractionClass>> fakeUDM = Isolate.Invoke.Method(fakeTNMapRelationshipObject,"get_UDM");
// make sure our code will return the relationship. Note we are chaining the calls - cool!
Isolate.WhenCalled(()=>fakeUDM.Interaction.PrimaryMember.PrimaryRelationships).WillReturn(primaryRe);
answered by scott (32k points)
...