Defining AddComment in a CommentController

 Given a CommentController
 When CreateComment
 Should setup the Comment with the values from the View

Test Code using Static Constructs

    [TestClass]
    public class Given_CommentController_When_CreateComment {

        readonly CommentController Subject = Create.TestObjectFor<CommentController>();

        [TestMethod]
        public void Should_setup_BlogEntry() {
            var ExpectedCommentBlogEntry = new BlogEntry();
            
            Given.That(Subject).View.BlogEntry = ExpectedCommentBlogEntry;
            Subject.CreateComment();
            Assert.AreSame(ExpectedCommentBlogEntry, Subject.Comment.BlogEntry);
        }

        [TestMethod]
        public void Should_setup_Content() {
            var ExpectedCommentContent = "comment content";

            Given.That(Subject).View.Content = ExpectedCommentContent;
            Subject.CreateComment();
            Assert.AreSame(ExpectedCommentContent, Subject.Comment.Content);
        }

        [TestMethod]
        public void Should_setup_Author() {
            var ExpectedCommentAuthor = "comment author";

            Given.That(Subject).View.Author = ExpectedCommentAuthor;
            Subject.CreateComment();
            Assert.AreSame(ExpectedCommentAuthor, Subject.Comment.Author);
        }
    }

Test Code using Extensions Constructs

    [TestClass]
    public class Given_CommentController_When_CreateComment {

        readonly CommentController Subject = Create.TestObjectFor<CommentController>();

        [TestMethod]
        public void Should_setup_BlogEntry() {
            var ExpectedCommentBlogEntry = new BlogEntry();
            
            Subject.Given().View.BlogEntry = ExpectedCommentBlogEntry;
            Subject.CreateComment();
            Assert.AreSame(ExpectedCommentBlogEntry, Subject.Comment.BlogEntry);
        }

        [TestMethod]
        public void Should_setup_Content() {
            var ExpectedCommentContent = "comment content";

            Subject.Given().View.Content = ExpectedCommentContent;
            Subject.CreateComment();
            Assert.AreSame(ExpectedCommentContent, Subject.Comment.Content);
        }

        [TestMethod]
        public void Should_setup_Author() {
            var ExpectedCommentAuthor = "comment author";

            Subject.Given().View.Author = ExpectedCommentAuthor;
            Subject.CreateComment();
            Assert.AreSame(ExpectedCommentAuthor, Subject.Comment.Author);
        }
    }

Production Code

    public class CommentController {

        public Comment View { get; set; }
        public Comment Comment { get; set; }

        public void CreateComment() { 
            Comment = Domain.ObjectFactory.NewComment(
                View.BlogEntry, View.Content, View.Author);
        }
    }

    public interface Comment {

        BlogEntry BlogEntry { get; set; }
        string Content { get; set; }
        string Author { get; set; }
    }

    public class BlogEntry { }

Last edited Nov 5, 2008 at 8:34 PM by mikemps, version 1

Comments

No comments yet.