Hi Kevin,
I am using SpecFlow for unit specs. I may have two answers for the
problems you described.
1. The way I try to shorted the text I have to type in each Given,
When, Then is by creating a container that contains short hands for
all the objects (mostly domain objects) that are used in the specs.
E.g. so that the example you gave would become
[When(@"I create an apartment")]
public void WhenICreateAnApartment()
{
Sc.Apartment = new Apartment(Sc.Address, Sc.Floor,
Sc.ApartmentNumber);
}
[Then(@"it should set the address")]
public void ThenItShouldSetTheAddress()
{
Sc.Address.ShouldEqual(Sc.Apartment.Address);
}
// Here is the shorthands for the current ScenarioContext to make the
above work
public static class Sc
{
public static Address Address
{
get { return (Address)ScenarioContext.Current["Address"]; }
set { ScenarioContext.Current["Address"] = value; }
}
public static Apartment Apartment
{
get { return (Apartment)ScenarioContext.Current["Apartment"]; }
set { ScenarioContext.Current["Apartment"] = value; }
}
public static int Floor
{
get { return (int)ScenarioContext.Current["Floor"]; }
set { ScenarioContext.Current["Floor"] = value; }
}
public static string ApartmentNumber
{
get { return
(Apartment)ScenarioContext.Current["ApartmentNumber"]; }
set { ScenarioContext.Current["ApartmentNumber"] = value; }
}
public static Exception Exception
{
get { return (Exception)ScenarioContext.Current["Exception"]; }
set { ScenarioContext.Current["Exception"] = value; }
}
}
Note that I name the shorthands as though they are unique in the whole
system (mostly using the ubiquitous language) to try to avoid
conflicts.
2. For exception, I use the Catch.Exception from MSpec.
[When(@"I create an apartment with invalid address")]
public void WhenICreateAnApartment()
{
Sc.Exception = Catch.Exception(() => Sc.Apartment = new
Apartment(Sc.Address, Sc.Floor, Sc.ApartmentNumber););
}
and I have steps that verify different exceptions. E.g.:
public virtual void
ThenShouldGetAPreconditionExceptionWithMessage(string message)
{
var preconditionException = Sc.Exception;
preconditionException.ShouldBeOfType(typeof(PreconditionException));
preconditionException.Message.ShouldEqual(message);
}
Hope this help. Please share if anyone have a better way. :)