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!

Need help in using Ioslator, getting null reference error

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\rajusi\My Documents\Visual Studio 2008\Projects\Phase2\CXM.BECU\CXM.BECU\CXM.BECU.Activity\Activities\Composites\TravelNotifications\TNMapRelationshipInfo.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\rajusi\My Documents\Visual Studio 2008\Projects\TestProject1\TestProject1\UnitTest1.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\rajusi\My Documents\Visual Studio 2008\Projects\TestProject1\TestProject1\UnitTest1.cs: line 0
asked Mar 2, 2012 by Sivakumar_Raju (360 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 Mar 4, 2012 by scott (29,080 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 Mar 5, 2012 by Sivakumar_Raju (360 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 Mar 5, 2012 by scott (29,080 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 Mar 5, 2012 by Sivakumar_Raju (360 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 Mar 5, 2012 by Sivakumar_Raju (360 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 Mar 5, 2012 by scott (29,080 points)
0 votes
Complete trace :




'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll'
'vstesthost.exe' (Managed): Loaded 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\vstesthost.exe'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Runtime.Remoting\2.0.0.0__b77a5c561934e089\System.Runtime.Remoting.dll'
'vstesthost.exe' (Managed): Loaded 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\Microsoft.VisualStudio.QualityTools.Common.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll'
'vstesthost.exe' (Managed): Loaded 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\Microsoft.VisualStudio.QualityTools.ExecutionCommon.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\TypeMock\6.2.5.0__3dae460033b8d8e2\TypeMock.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Typemock.Interceptors\6.2.5.0__3dae460033b8d8e2\Typemock.Interceptors.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.QualityTools.Resource\9.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.QualityTools.Resource.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll'
'vstesthost.exe' (Managed): Loaded 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\Microsoft.VisualStudio.QualityTools.ControllerObject.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll'
'vstesthost.exe' (Managed): Loaded 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\Microsoft.VisualStudio.QualityTools.AgentObject.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll'
'vstesthost.exe' (Managed): Loaded 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.QualityTools.UnitTestFramework\9.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll'
The thread 0x818 has exited with code 0 (0x0).
'vstesthost.exe' (Managed): Loaded 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter.dll'
'vstesthost.exe' (Managed): Loaded 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\Microsoft.VisualStudio.QualityTools.Tips.UnitTest.AssemblyResolver.dll'
'vstesthost.exe' (Managed): Loaded 'C:\Documents and Settings\rajusi\My Documents\Visual Studio 2008\Projects\Phase2\CXM.BECU\TestResults\rajusi_WS-39700 2012-03-05 07_46_56\Out\TestProject1.dll', Symbols loaded.
'vstesthost.exe' (Managed): Loaded 'C:\Documents and Settings\rajusi\My Documents\Visual Studio 2008\Projects\Phase2\CXM.BECU\TestResults\rajusi_WS-39700 2012-03-05 07_46_56\Out\Typemock.ArrangeActAssert.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Configuration\6.2.5.0__3dae460033b8d8e2\Configuration.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\CXM.Rudder.DataModel\7.0.0.35__89cd864dc3276342\CXM.Rudder.DataModel.dll'
'vstesthost.exe' (Managed): Loaded 'C:\Documents and Settings\rajusi\My Documents\Visual Studio 2008\Projects\Phase2\CXM.BECU\TestResults\rajusi_WS-39700 2012-03-05 07_46_56\Out\CXM.BECU.DataModel.dll', Symbols loaded.
'vstesthost.exe' (Managed): Loaded 'C:\Documents and Settings\rajusi\My Documents\Visual Studio 2008\Projects\Phase2\CXM.BECU\TestResults\rajusi_WS-39700 2012-03-05 07_46_56\Out\CXM.BECU.Activity.dll', Symbols loaded.
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\SOAdesk.SUDS.Rudder.Workflow.Shared\7.0.0.35__89cd864dc3276342\SOAdesk.SUDS.Rudder.Workflow.Shared.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Workflow.ComponentModel\3.0.0.0__31bf3856ad364e35\System.Workflow.ComponentModel.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\SOAdesk.Framework\7.0.0.35__89cd864dc3276342\SOAdesk.Framework.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\CXM.Rudder.Shared\7.0.0.35__89cd864dc3276342\CXM.Rudder.Shared.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\WindowsBase\3.0.0.0__31bf3856ad364e35\WindowsBase.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\9.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'
'vstesthost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Workflow.Runtime\3.0.0.0__31bf3856ad364e35\System.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 Mar 5, 2012 by Sivakumar_Raju (360 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 Mar 5, 2012 by Sivakumar_Raju (360 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 Mar 5, 2012 by Sivakumar_Raju (360 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 Mar 6, 2012 by scott (29,080 points)
...