Defining AddComment in a CommentController

 Given a CommentController
 When adding a Comment
 Should create the Comment
 Should add the Comment to the Repository

. Add a reference to fluentspec.dll in the project

. Create a new test class. (using MSTest in this sample)
. To enable the Fluent Syntax make the TestClass a BehaviorOf<SUT> (SUT => System Under Test)

    [TestClass]
    public class Given_CommentController : BehaviorOf<CommentController> { }

. Create the CommentController class to make the test pass

    public class CommentController {}

. Test AddComment creates the Comment

    [TestClass]
    public class Given_CommentController : BehaviorOf<CommentController> {

        [TestMethod]
        public void When_AddComment_Should_CreateComment() {

            When.AddComment(); 
            Should.CreateComment();
        }

    }

. Write enough code to make the test pass

    public class CommentController {

        public void AddComment() {
            CreateComment();
        }

        public virtual void CreateComment() { }

    }

. Test Comment is added to the Repository

    [TestClass]
    public class Given_CommentController : BehaviorOf<CommentController> {

        [TestMethod]
        public void When_AddComment_Should_CreateComment() {

            When.AddComment(); 
            Should.CreateComment();
        }

        [TestMethod]
        public void When_AddComment_Should_AddCommentToRepo() {

            When.AddComment(); 
            Should.AddCommentToRepo();
        }     
    }

. Write enough code to make the test pass

    public class CommentController {

        public void AddComment() {
            CreateComment();
            AddCommentToRepo();
        }

        public virtual void CreateComment() { }

        public virtual void AddCommentToRepo() { }

    }

. Refactor

    [TestClass]
    public class Given_CommentController_When_AddComment : BehaviorOf<CommentController> {

        [TestInitialize]
        public void Setup() {
        
            When.AddComment();
        }

        [TestMethod]
        public void Should_CreateComment() {

            Should.CreateComment();
        }

        [TestMethod]
        public void Should_AddCommentToRepo() {

            Should.AddCommentToRepo();
        }        
    }

Last edited Nov 5, 2008 at 4:51 AM by mikemps, version 1

Comments

No comments yet.