.NET Fit patch: corrected error message on invalid fixture type

Daniel Gackle <[email protected]> Thu, 07 Aug 2003 17:54:44 -0600
Newsgroups gmane.comp.programming.tools.fit.devel
Message-ID <[email protected]>
Jim,

Here's a change we made to the .NET version of Fit during Ward's seminar at
the Agile Development Conference in Salt Lake.  This is from the
loadFixture() method in Fixture.cs.  (This source came from
http://fit.c2.com/files/DotNetPlatform/Quick-Start-Fit.zip and the date on
the file is 4/25/2003.)

The code before we changed it did this:

//
BEFORE ---------------------------------------------------------------------
-
public Fixture loadFixture(string className)
{
	foreach (string assemblyName in assemblies)
	{
		Assembly assembly = Assembly.LoadFrom(assemblyName);

		// We are getting the correct fit.ActionFixture object instatiated here
...
		object obj = assembly.CreateInstance(className);

		// But we are failing to cast the fit.ActionFixture as a Fixture here ...
		Fixture fixture = (obj as Fixture);

		if (fixture != null) return fixture;
	}
	throw new ApplicationException("Fixture '" + className + "' could not be
found in assemblies");
}
//
BEFORE ---------------------------------------------------------------------
-

When the call to CreateInstance created an instance of our type, but our
type didn't inherit from Fixture, the expression "obj as Fixture" was null,
so the ApplicationException got thrown.  The trouble is that the error
message states incorrectly that the type couldn't be found.  In fact it was
being found with no trouble- it just wasn't the right type.  This caused us
to waste some time solving the wrong problem.  Once we figured this out, we
changed the code as follows:

//
AFTER ----------------------------------------------------------------------
public Fixture loadFixture(string className)
{
	foreach (string assemblyName in assemblies)
	{
		Assembly assembly = Assembly.LoadFrom(assemblyName);

		object obj = assembly.CreateInstance(className);

		if (obj != null)
		{
			return (Fixture) obj; // <------ throws an InvalidCastException if we
didn't get a Fixture
		}
	}
	throw new ApplicationException("Fixture '" + className + "' could not be
found in assemblies");
}
//
AFTER ----------------------------------------------------------------------

This distinguishes the two errors.  If the object gets created but isn't a
Fixture, then .NET will throw an InvalidCastException on the line indicated.
If it can't be created at all, you get the ApplicationException.  Hope this
helps.

Regards,
Daniel

-----Original Message-----
From: [email protected]
[mailto:[email protected]]On Behalf Of Jim Little
Sent: Thursday, August 07, 2003 5:21 PM
To: [email protected]
Subject: [Fit-dev] .NET Fit now in CVS

[...]

> Now that I've got the basics set up and working, I'm going to start
bringing the .NET version up to date.  If you have any requests or patches,
please send them in now.  Don't assume that I've seen prior posts.

[...]

Thanks,
Jim