How to verify an object argument without having to manually check with Matching

0 votes

I want to create a verification that will accept an object with its properties set up to match an object that will be provided in the code being tested.  I am trying to avoid having to write a .Matching() statement and manually compare the values.  

So this is the object in the method under test that will be submitted:

:
:
MiscPayment miscPayment = new MiscPayment 
{ 
	// these come from arguments earlier in the method
	PatientID = patientID, 
	MiscPaymentTypeID = miscPaymentType,
	OriginalOrderNumber = originalOrderNumber,
	OriginalStoreNumber = originalStoreNumber
};
:
:
:
// then the transactionId will be updated
miscPayment.TransactionId = transaction.ID;
_PosTransactionServices.SaveMiscPayment(miscPayment);
 

In my unit test, I create this object as the comparison:

var fakeMiscPayment = Isolate.Fake.Instance<MiscPayment>(Members.CallOriginal);
fakeMiscPayment.PatientID = patientId;
fakeMiscPayment.MiscPaymentTypeID = miscPaymentTypeId;
fakeMiscPayment.OriginalOrderNumber = originalOrderNumber;
fakeMiscPayment.OriginalStoreNumber = originalStoreNumber;
fakeMiscPayment.TransactionId = transactionId;
Isolate.Swap.AllInstances<MiscPayment>().With(fakeMiscPayment);

And in my Assert section, I want to call this and have it just compare the properties of fakeMiscPayment and MiscPayment (that was submitted in the SaveMiscPayment() method).

Isolate.Verify.WasCalledWithExactArguments(() => fakeTransactionServices.SaveMiscPayment(fakeMiscPayment));

When I do this, I get this error:

Failure:
TypeMock.VerifyException : 
TypeMock Verification: Method ITransactionServices.SaveMiscPayment(MiscPayment) was called with mismatching arguments
 
Expected: SaveMiscPayment(IT2.Core.POS.MiscPayment)
Actual:   SaveMiscPayment(IT2.Core.POS.MiscPayment)
--------------------------^
The following properties are not equal on the object being passed as argument #1 (of type PosMiscPayment)
ErrorMessages:
   Expected: {  }
   Actual:   {  }
_____

It doesnt actually say what the issue is.  

How can I get this to work?

asked Feb 15 by jim.little (250 points)

1 Answer

+1 vote
Hi,

The WasCalledWithExactArguments API uses Equals to compare the two object.

You can either implement your Equals in MiscPayment or use .Matching()
answered Feb 19 by eli (5,770 points)
...