TDD外部进程?

问题描述:

让我们假设我已经创建了下面的测试我的示例应用程序博客:TDD外部进程?

[TestClass] 
public class when_the_blog_controller_index_action_executes : BlogControllerTests 
{ 
     //... 

    [TestMethod] 
    [TestCategory("BlogController")] 
    public void it_should_pass_the_latest_blogentries_to_the_view() 
    { 
     blogs = new List<BlogEntry>() { new BlogEntry("title1", "b1"), new BlogEntry("title2", "b2"), new BlogEntry("title3", "b3") }; 
     blogServiceMock = new Mock<IBlogService>(); 

     blogServiceMock.Setup(s => s.GetLatestBlogEntries()) 
         .Returns(blogs); 

     var controller = new BlogController(blogServiceMock.Object); 

     var model = ((ViewResult)controller.Index()).Model as IEnumerable<BlogEntry>; 

     Assert.IsTrue(blogs.SequenceEqual(model)); 
     blogServiceMock.VerifyAll(); 
    } 
} 

的BlogController实施后,我有一个正在运行的测试:

public class BlogController : Controller 
{ 
    IBlogService blogService; 

    public BlogController(IBlogService blogService) 
    { 
     this.blogService = blogService; 
    } 

    public ActionResult Index() 
    { 
     var model = blogService.GetLatestBlogEntries(); 

     return View(model); 
    } 
} 

那么,什么是下一个步骤?我应该为IBlogService创建实现(使用测试)并在创建存储库(使用测试)之后? 如果我有这样的服务,基本上我不会测试任何东西,因为我只是嘲笑库...

public class BlogService : IBlogService 
{ 
    IBlogRepository blogRepository; 

    public BlogService(IBlogRepository blogRepository) 
    { 
     this.blogRepository = blogRepository; 
    } 

    public IEnumerable<BlogEntry> GetLatestBlogEntries() 
    { 
     return blogRepository.GetLatestBlogEntries(); 
    } 
} 

下一步是进行单元测试你写的BlogService实施。在这个测试中,你应该确保正确的方法在模拟库上被调用。如果以后你的服务逻辑发展了,并且你有一些方法不仅仅执行CRUD存储库访问,你的测试将开始变得更有意义。

你当前的这个服务的实现只是将调用委托给知识库,这就是你应该单元测试的东西。

如果您发现自己的服务层由简单的CRUD方法组成,这些方法无非是将调用委托给某个存储库,那么您应该问问自己这个服务层的用处以及它是否也不会可以直接从控制器使用存储库。

> TDD Process Outside-In? 

我会同时使用两个嵌套循环从外到内(BDD)的结构和内而外(TDD)在Behavior-Driven Development with SpecFlow and WatiN

* writing a failing (outside-in) integration tests 
    * writing a failing (inside out) unit test as part of the solution of the integration test 
     * making the unittest pass 
     * refactor 
    * writing the next failing unit test as part of the integration test 

    * unitl the integration test passes 

* writing the next failing integration tests 
+0

感谢描述我会检查这个工具 – Peti 2011-05-20 16:22:11

+0

是不是那个重要的工具,而是它背后的工作流程 – k3b 2011-05-23 07:59:12