About the author

Steven HarmanSteven Harman is a passionate developer who believes that writing great software isn't just a job, its a craft.

ASP.NET MVP

For recent posts and more about me, scroll to the bottom.

Subscribe

  • Subscribe to my feed. via RSS
  • Subscribe via email via email

Jobs

Badges

  • Subtext Project
  • Support Subtext
  • HiddenNetwork.com Banner

Saving the World via… TDD?

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:

  1. Shows the gist of Context/Specification style specs in use.
  2. 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.

kick it on DotNetKicks.com

What others are saying.

# re: Saving the World via… TDD?
Gravatar Haacked
Jan 14, 2009
The problem with the first test is that unit tests should be repeatable. Did you try running that test 1000 times? Perhaps on the 1001th time it fails, thus ensuing doom for all the rest of us. ;)
# re: Saving the World via… TDD?
Gravatar Steven Harman
Jan 14, 2009
@Haacked,
Nay my friend... we're not doomed. For even if that test were to fail do to some change in the implementation, we still have John Connor! :)
# re: Saving the World via… TDD?
Gravatar Jake
Jan 16, 2009
sorry for such a noob question but MockRepository.GenerateStub vs MockRepository.GenerateMock question. What are the real differences? I know that you can set expectatins on Mocked objects and that they can be verfified, so then is a stub just a way of setting up certain behaviours on objects so that you can test your subject under specifc states?

Also is the MockRepository.GenerateMock<> the new syntax for Rhino mocks? I have only really played around with Moq so Rhino mocks can be hard to understand :)
# re: Saving the World via… TDD?
Gravatar SuperJason
Jan 16, 2009
Don't forget to check your code coverage to make sure there are no untested paths.
# re: Saving the World via… TDD?
Gravatar Colin Jack
Jan 21, 2009
Only thing, isn't this ending up testing against a stub.

Way to miss the point Colin you might reply though :P
Comments have been closed on this topic.