Hot on the heels of my wildly (in)famous “When Should I Write Tests?” post, I have another fun tidbit about testing to share with you. Though, to be honest, I found this gem via my buddy Scott C Reynolds, so I can’t take all – actually, I can’t take any – of the credit.
Anyhow, Scott posted a great little snippet of code that does a couple of things:
- Shows the gist of Context/Specification style specs in use.
- Ensures we won’t need John Connor’s help after all.
using Skynet.Core
public class when_initializing_core_module
{
ISkynetMasterController _skynet;
public void establish_context()
{
//we'll stub it...you know...just in case
_skynet = new MockRepository.GenerateStub<ISkynetMasterController>();
_skynet.Initialize();
}
public void it_should_not_become_self_aware()
{
_skynet.AssertWasNotCalled(x => x.InitializeAutonomousExecutionMode());
}
public void it_should_default_to_human_friendly_mode()
{
_skynet.AssessHumans().ShouldEqual(RelationshipTypes.Friendly);
}
}
public class when_attempting_to_wage_war_on_humans
{
ISkynetMasterController _skynet;
public void establish_context()
{
_skynet = new MockRepository.GenerateStub<ISkynetMasterController>();
_skynet.Stub(x =>
x.DeployRobotArmy(TargetTypes.Humans)).Throws<OperationInvalidException>();
}
public void because()
{
_skynet.DeployRobotArmy(TargetTypes.Humans);
}
public void it_should_not_allow_the_operation_to_succeed()
{
_skynet.AssertWasThrown<OperationInvalidException>();
}
}
Originally I wasn’t going to repost the code here because I didn’t want to take credit for it. But after a quick chat with Scott I decided maybe I should, just in case Pastie decides to take a dump! Plus, this is like a little time capsule of our current thoughts on Context/Specification, pop culture, and the ceremony of many of the languages we currently use.
But as a matter of full disclosure, here is a link to the original code on pastie.org.
