Domanda

I have two extension methods:

public static IPropertyAssertions<T> ShouldHave<T>(this T subject)
{
    return new PropertyAssertions<T>(subject);
}

public static IPropertyAssertions<T> ShouldHave<T>(this IEnumerable<T> subject)
{
    return new CollectionPropertyAssertions<T>(subject);
}

Now I write some code which uses it:

List<Customer> collection2 = new List<Customer>(); 
collection2.ShouldHave(); //first overload is chosen
IEnumerable<Customer> collection3 = new List<Customer>(); 
collection3.ShouldHave(); //second overload is chosen

Second overload is chosen only if I explicitly specify IEnumerable type. Is there any way to make second overload to be chosen in both cases?

È stato utile?

Soluzione

In Fluent Assertion 2.0 I've finally managed to solve the above problems in a decent way. Read all about that here: http://www.dennisdoomen.net/2012/09/asserting-object-graph-equivalence.html

Altri suggerimenti

The first overload is a better match, because T is inferred as List<Customer>, which gives an exact match. For the second overload, it would infer T as Customer, so the parameter would be IEnumerable<Customer>, which is a less exact match than List<Customer>.

Don't think so. Don't think it's possible that in this case IEnumerable<T> overload will be always called, as it has less accurate match in regard of T type. If you don't specify a concrete IEnumerable<T>, the best match will be always a first method, in this case.

ShouldHave() has a double meaning. In the first case, ShouldHave() has a return about the object supplied by the subject-parameter. In the second case, the return is about items in the enumeration, and not about the enumeration itself.

In case I create my own collection and I want to test this collection itself (not the items), I certainly want ShouldHave(this T subject) to be called and not ShouldHave(this IEnumerable subject).

Maybe you should reconsider your design. The second ShouldHave() does two things, so should be split into a method that extracts the collection-items and a call to the first ShouldHave(), that you already have.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top