NSpecify Nunit add-in fix for InitializeFunctionality methods
Well I still exist!
Recently I’ve been moving more and more into understanding how a specification test should be written using NSpecify a really nice framework for running tests.
Also I’ve been seriously involved in getting the teams build process up and running in a much more meaningful form, as such I’ve noticed a bug in the nunit add-in for NSpecify.
It wasn’t running any of my InitializeFunctionality (Incidently i think this would be better named as ContextSetup and Functionality renamed to Context) methods.
I looked in the code and realised none of the setup’s or teardowns were getting wired up, a quick modification and there all hunky dory.
2 changes are required:
NSpecifySuiteExtension.cs should look like this
using System;
using System.Reflection;
namespace NUnit.Core.Extensions
{
/// <summary>
/// NSpecifySuiteExtension extends test suite and creates a fixture that runs every specification implementing the NSpecify attributes.
/// Because it inherits from TestSuite, rather than TestFixture, it has to construct its own fixture object and find its
/// own specifications. Everything is done in the constructor for simplicity.
/// </summary>
internal class NSpecifySuiteExtension : TestSuite
{
#region C'tors
/// <summary>
/// Initializes a new instance of the <see cref="NSpecifySuiteExtension"/> class.
/// </summary>
/// <param name="fixtureType">Type of the fixture.</param>
public NSpecifySuiteExtension(Type fixtureType)
: base(fixtureType)
{
// Create the fixture object. We could wait to do this when
// it is needed, but we do it here for simplicity.
Fixture = Reflect.Construct(fixtureType);
fixtureSetUp = Reflect.GetMethodWithAttribute(fixtureType, Constants.InitializeFunctionalityAttribute, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, true);
fixtureTearDown = Reflect.GetMethodWithAttribute(fixtureType, Constants.CleanupFunctionalityAttribute, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, true);
// Locate our test methods and add them to the suite using
// the Add method of TestSuite. Note that we don't do a simple
// Tests.Add, because that wouldn't set the parent of the tests.
foreach (MethodInfo method in fixtureType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
Attribute spec = Reflect.GetAttribute(method, Constants.SpecificationAttribute, true);
if (spec != null)
{
Add(new NSpeicifyTestMethod(method));
}
}
}
#endregion
}
internal class NSpeicifyTestMethod : NUnitTestMethod
{
public NSpeicifyTestMethod(MethodInfo method)
: base(method)
{
setUpMethod = Reflect.GetMethodWithAttribute(FixtureType, Constants.SetupResourcesAttribute, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, true);
tearDownMethod = Reflect.GetMethodWithAttribute(FixtureType, Constants.DestroyResourcesAttribute, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, true);
}
}
}
And constants.cs just gets updated to this:
namespace NUnit.Core.Extensions
{
/// <summary>
/// The constants
/// </summary>
public class Constants
{
public const string FunctionalityAttribute = "NSpecify.Framework.FunctionalityAttribute";
public const string InitializeFunctionalityAttribute = "NSpecify.Framework.InitializeFunctionalityAttribute";
public const string CleanupFunctionalityAttribute = "NSpecify.Framework.CleanupFunctionalityAttribute";
public const string SpecificationAttribute = "NSpecify.Framework.SpecificationAttribute";
public const string SetupResourcesAttribute = "NSpecify.Framework.SetupResourcesAttribute";
public const string DestroyResourcesAttribute = "NSpecify.Framework.DestroyResourcesAttribute";
public const string SuiteBuilders = "SuiteBuilders";
}
}
I’ve got a patch file if I can get the people who have write access to the NSpecify repository to apply it (there are no mailing lists for me to send it to)
-
bennage