4
\$\begingroup\$

I have created an extension for unit-testing my own equality implementations. Is it a good approach in general to have such extensions for unit testing? What unit-testing extensions do you use?

public static class EqualityExtensions
    {
        /// <summary>
        /// Checks that all variations Equals method conforms to equality by properties
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="variations"></param>
        /// <param name="propertyAccessors">Property accessors specifying which properties contribute to equality</param>
        /// <returns>True, if all instances from a set conform to property equality</returns>
        /// <example>
        /// Assert.IsTrue(from name in new[] {"foo","bar"} from count in new[] {1,2} select new MyClass{Name=name,Count=count}).ConformToPropertyEquality(x=>x.Name,x=>x.Count))
        /// </example>
        public static bool ConformToPropertyEquality<T>(this IEnumerable<T> variations, params Func<T, object>[] propertyAccessors)
        {
            var propertyComparisions =
                from lhs in variations
                from rhs in variations
                select
                    Equals(
                        (from comparator in propertyAccessors select Equals(comparator(lhs), comparator(rhs))).All(c => c),
                        Equals(lhs, rhs)
                    );

            return propertyComparisions.All(p => p);
        }
    }
\$\endgroup\$
1
\$\begingroup\$

It's perfectly OK. As an example SharpTestEx is a whole library with such an extensions.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.