It's more general.  Basically you should be able to test any class that
implements IEnumerable if you can supply a suitable IEnumerableBuilder.
The builder allows you to set up the state of the object being tested in
a very general way.

An equivalent solution would be to have multiple factories with named
outputs for your different tests.  Like GetEnumerableWithNoElements(),
GetEnumerableWithThreeElements() and so on.  The builder introduces less
coupling between the tests and the embedding of the object under test
because of its constructive approach to obtaining objects in various
states.  The object doesn't need to be created until you call
GetResult() so the builder can handle immutable objects too.

Adding an AddItem<T> method works as well but then you have to subclass
the test fixture if you want to reuse it with different types.


So basically what I envision is that we could cook up a barrage of tests
for common types and fully abstract out the code used to manufacture
objects they consume in various states.  This goes beyond what can
reasonably be achieved by TypeFixture's plain old factory.  Notably you
don't need to create multiple fixtures to test objects in multiple
states.  Also you can refer to the tester from multiple fixtures.

So here's something closer to what I have in mind.  I'm sure it can be
simplified in many ways.


public interface IEnumerableBuilder<T>
{
    void AddItem(T item);
    IEnumerable<T> GetResult();
}

// The tester, notice this fixture defines a few properties but there
// are no values bound to them.  This is a data driven test fixture.
// In the absence of bindings it does nothing.
[TestFixture]
public class IEnumerableTester<T>
{
    [TestProperty]
    public Action<IEnumerableBuilder<T>> BuilderFactory;

    [TestProperty]
    public Action<T> ItemFactory;

    [Test]
    public void EnumerableWithNoItems()
    {
        IEnumerableBuilder<T> builder = BuilderFactory();
        IEnumerable<T> enumerable = builder.GetResult();

        // etc... as before
    }
}

[TestFixture]
public class TestFancyCollection
{
    // Initialize the fixture and bind its parameters.
    // Run it as an ordinary test suite.
    // Note: There are other ways we could do this so I'm still working
on it.
    //       I actually have in mind an attribute based data-binding
syntax.
    [TestSuite]
    public IEnumerableTester<int> EnumerableTests()
    {
        IEnumerableTester<int> tester = new IEnumerableTester<int>();

        int nextValue = 1;
        tester.BuilderFactory = delegate { return new
EnumerableBuilder<int>(); };
        tester.ItemFactory = delegate { return nextValue++; };
    }

    private class EnumerableBuilder<T> : IEnumerableBuilder<T>
    {
        private FancyCollection<T> collection;

        public void AddItem(T item)
        {
            collection.Add(item);
        }

        public IEnumerable<T> GetResult()
        {
            return collection;
        }
    }
}


Jeff. 

-----Original Message-----
From: [email protected] [mailto:[EMAIL PROTECTED]
On Behalf Of Morten Mertner
Sent: Thursday, September 14, 2006 5:12 PM
To: [email protected]
Subject: MbUnit Re: TypeFixture on steroids was RE: MbUnit Re: Research:
Using LSP to ease testing


Hi Jeff,

> Instead of using one factory to manufacture instances for all tests, 
> provide a builder that can manufacture instances that satisfy the 
> specifications for multiple tests by application of the specified 
> construction rules.
>   
I am not sure if this is actually helpful, apart from allowing you to
populate the underlying implementation of IEnumerable with a known set
of items. You still need knowledge of the implementation in order to be
able to add items - effectively you've just moved the code into an
associated builder class.

However, in the context of writing reusable test cases this does become
useful. I'd probably have gone for a simple AddItem<T>( T item ) helper
method on the test class though.

Yours,
Morten




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"MbUnit.User" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/MbUnitUser
-~----------~----~----~----~------~----~------~--~---

Reply via email to