public static SamePropertyObject <T> Except <T>(this AssertObject <T> assertObject, Expression <Func <T, object> > excludedProperty) { var assertSamePropertyObject = new SamePropertyObject <T>(assertObject.Object); assertSamePropertyObject.Exclusions.Add(excludedProperty); return(assertSamePropertyObject); }
/// <summary> /// Verify that two objects have the same properties, this will ignore the comparison for objects and collections /// </summary> /// <param name="assertObject">Assert object</param> /// <param name="comparedObject">Object to be compared with</param> /// <param name="exclusions">String array with the list of properties to not compare</param> public static AssertObject <T> HasSameProperties <T>(this AssertObject <T> assertObject, object comparedObject, params string[] exclusions) { Assert.IsNotNull(assertObject.Object); Assert.IsNotNull(comparedObject); foreach (var propertyInfo in assertObject.Object.GetType().GetProperties()) { // Exclude properties if (exclusions != null && ((IList)exclusions).Contains(propertyInfo.Name)) { continue; } // Ignore Objects and Collections if (propertyInfo.PropertyType.GetTypeInfo().IsValueType || propertyInfo.PropertyType == typeof(string)) { var objectValue = assertObject.Object.GetType().GetProperty(propertyInfo.Name).GetValue(assertObject.Object, null); var comparedObjectValue = comparedObject.GetType().GetProperty(propertyInfo.Name).GetValue(comparedObject, null); if (objectValue is DateTime) { TimeSpan difference = (DateTime)objectValue - (DateTime)comparedObjectValue; Assert.IsTrue(difference < TimeSpan.FromSeconds(1), $"Property '{propertyInfo.Name}' of type DateTime has value {objectValue} but was expected {comparedObjectValue}"); continue; } Assert.AreEqual(objectValue, comparedObjectValue, $"Property '{propertyInfo.Name}' of type {assertObject.Object.GetType()} has value {objectValue} but was expected {comparedObjectValue}"); } } return(assertObject); }
/// <summary> /// Verify that the object has the desired type /// </summary> /// <typeparam name="T">Type of the object</typeparam> /// <typeparam name="O">Expected Object type to verify</typeparam> /// <param name="assertObject">Assert object</param> public static void IsOfType <T, O>(this AssertObject <O> assertObject, string message = null) { if (assertObject.Object is T) { return; } throw new AssertFailedException(message ?? $"Expected type {typeof(T)} but was {assertObject.GetType()}"); }
public static AssertObject <T> IsTrue <T>(this AssertObject <T> assertObject, string message = null) { Assert.IsNotNull(assertObject.Object, message ?? "The object is null"); return(assertObject); }
public static AssertObject <T> HasValue <T>(this AssertObject <T> assertObject, object value, string message = null) { Assert.AreEqual(assertObject.Object, value, message); return(assertObject); }
public static AssertObject <T> And <T>(this AssertObject <T> assertObject) { return(assertObject); }
public static AssertObject <T> Has <T>(this AssertObject <T> assertObject, Func <T, bool> assertions) { Assert.IsTrue(assertions(assertObject.Object)); return(assertObject); }