Does MS Test provide a default value equals comparison?
I want to test for example
int orderId = myRepository.SubmitOrder(orderA);
orderB = myRepository.GetOrder(orderId);
Assert.AreEqual(orderA, orderB); // fail
Obviously I need a value comparison here, but I don't want to have to provide an overridden Equals implementation for all of my classes purely for the sake of testing (it wouldn't be of any use in the rest of the app).
Is there a provided generic method that just checks every field using reflection? Or if not, it is possible to write my own?
EDIT: As it seems people are kind of missing the point. I don't want to have to write my own comparison logic. That would require hundreds of lines of extra code. I'm looking for something like a generic
bool ContainSameValues<T&开发者_C百科gt;(T t1, T t2)
method which recursively uses reflection to pull out all the values in T.
FURTHER EDIT: Since it doesn't appear there is any built in support for doing something like this, you can see my (failed) attempt at writing my own here
Easiest thing to do is compare the "primitive" fields yourself:
Assert.AreEqual(orderA.ID, orderB.ID);
Assert.AreEqual(orderA.CustomerID, orderB.CustomerID);
Assert.AreEqual(orderA.OrderDate, orderB.OrderDate);
As the Assert
class is static, it is impossible to create extension methods on it (as an instance is required). However, why not create a wrapper for the Assert
class, where you can perform custom assertions?
e.g:
public static class MyAssertions
{
public static void AreOrdersEqual(Order a, Order b)
{
if (!OrdersAreEqual) // your comparison logic here
Assert.Fail("Orders arent equal");
}
}
Then in your test:
MyAssertions.AreOrdersEqual(orderA, orderB)
You'll have to implement IComparable(or ICompare?) in the Order class.method.
精彩评论