Does AutoFixture.Idioms provide a way to handle excluding some items from Assertions?

Aug 10, 2012 at 9:50 AM

Hi, have started using AutoFixture.Idioms. I generally do something like:

var fixture = new Fixture();

var assertion = new GuardClauseAssertion( fixture.Customize( new AutoMoqCustomization() ), new NullReferenceBehaviorExpectation() );

assertion.Verify( typeof( Domain.Module ).Assembly.GetTypes().Where( x => x.Name != typeof( Module ).Name ) );


IOW what I end up wanting to say is "do the assembly but skip the Module class" in a succinct way

It would seem that for all but simple cases (OK, proper code where one has obeyed the rules :P) if Idioms is to offer a way to run assertions at multiple levels, it should also provide ways of filtering them out via a predicate. Perhaps this is there (didnt look TBH).

Other than that, it's a great feeling deleting Guard Clause Tests and make tests suites more maintainable - thanks!

(Now, if only CodeRush's templates didn't generate such horrible guard clauses (String.IfNullOrEmpty, redundant messages etc.))

Coordinator
Aug 10, 2012 at 3:25 PM

Actually, you are using it just as intended :)

I briefly thought about building something like this into the API, but then I thought that no matter what, it was always going to be a weak and less feature-rich version of what you can do with Reflection and LINQ.

If you want a more readable test, you could always encapsulate the expression into helper method or (my favorite option) class.

While I haven't tried to compile the following (I'm writing the code directly in the editor here), this is what I have in mind:

public class AllTypesExceptModule : IEnumerable<Type>
{
    public IEnumerator<Type> GetEnumerator()
    {
        typeof(Domain.Module)
            .Assembly
            .GetTypes()
            .Where(x => x.Name != typeof(Module).Name))
            .GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}

This would enable you to write an assertion like this:

assertion.Verify(new AllTypesExceptModule());

Aug 11, 2012 at 11:47 PM

Thanks for the response; Nice pattern worth hilighting (/naming!) which now you mention it you use variants of to good effect around AF.

There's a lot of truth in not attempting to duplicate LINQ to objects in an undefined/probably undefinable space (esp too early). But I can't resist thinking aloud:-

While the name of the class conveys my intent exactly, I do wonder whether there's a place for some more-work-to-implement-than-ya-think fluent API that lets me say things like:

The assembly (excluding this kind of Type) 

The type (excluding this kind of MemberInfo)

The assembly but for this Type I want to ignore method X

 

The assembly but for all Types I want to ignore method's matching Format X

.... etc.

Maybe something simple like being able to slot in predicates to filter the type list to say "And I'll tell you about what we're doing with those guys in a minute" without getting into an unwieldy non-composable fluent syntax. And then one has a similar Do Type X except methods matching this predicate.

Yes, it appears I have answered my own question - hard to imagine an API that doesnt get messy. Ninject.Extensions.Conventions almost gets there (I'm sure every other DI Container on the planet (OK, except 2!) has an equivalent set)

I guess I'll try to come up with actual use cases over time and who knows, might even get around to spewing them here over time.