Friday, February 10, 2006

RowEntryFixture for .Net FitNesse

Many acceptance tests need test data to work with, and RowEntryFixture is a simple fixture made for just that. You create a class that inherits from RowEntryFixture and override its EnterRow method to add an item of data from the corresponding table in your acceptance test.

When I started using FitNesse for my .Net projects, however, I discovered that the FitNesse developers didn't add RowEntryFixture to the .Net part of FitNesse. So I ported the Java version to .Net, and I built it into my .Net FitNesse replacement bundle. Here's the code for anyone wanting to build FitNesse with it:

using System;
using System.IO;

using fit;

namespace fitnesse.fixtures
{
    public abstract class RowEntryFixture : ColumnFixture
    {
        public abstract void EnterRow();

        public const String ERROR_INDICATOR = "Unable to enter last row: ";
        public const String RIGHT_STYLE = "pass";
        public const String WRONG_STYLE = "fail";

        public override void DoRow(Parse row)
        {
            if(row.Parts.Body.IndexOf(ERROR_INDICATOR) != -1)
                return;

            base.DoRow(row);
            try
            {
                EnterRow();
                Right(AppendCell(row, "entered"));
            }
            catch(Exception e)
            {
                Wrong(AppendCell(row, "skipped"));
                ReportError(row, e);
            }
        }

        protected Parse AppendCell(Parse row, String text)
        {
            Parse lastCell = new Parse("td", text, null, null);
            row.Parts.Last.More = lastCell;
            return lastCell;
        }

        public void ReportError(Parse row, Exception e)
        {
            Parse errorCell = MakeMessageCell(e);
            InsertRowAfter(row, new Parse("tr", null, errorCell, null));
        }

        public Parse MakeMessageCell(Exception e)
        {
            Parse errorCell = new Parse("td", "", null, null);
            StringWriter buffer = new StringWriter();

            buffer.Write(e.StackTrace);
            errorCell.AddToTag(" colspan=\"" + (ColumnBindings.Length + 1) + "\"");
            errorCell.AddToBody("<i>" + ERROR_INDICATOR + e.Message + "</i>");
            errorCell.AddToBody("<pre>" + (buffer.ToString()) + "</pre>");
            Wrong(errorCell);

            return errorCell;
        }

        public void InsertRowAfter(Parse currentRow, Parse rowToAdd)
        {
            Parse nextRow = currentRow.More;
            currentRow.More = rowToAdd;
            rowToAdd.More = nextRow;
        }

    }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.