当它们是对象依赖时,如何在编写源代码之前编写测试代码? [英] How to write test code before write source code when they are objects dependencies?

查看:35
本文介绍了当它们是对象依赖时,如何在编写源代码之前编写测试代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我举个例子:
要求:按照以下规则构建一块材料:
在给定的
100 => 材料是绿色的
100<其中<300=>材质是黄色
其中 > 300=> 材质为红色

I'l will take an example:
requirement: construct a piece of material following these rule:
among given < 100 => material is green
100 < among <300=> material is yellow
among > 300=> material is red

我的测试班:

        class MaterialTest{
          MaterialConstructor materialCons
          public void testBuild(){
                 materialCons.setAmount(150);
                 Material material=materialCons.build();
                 assertEqual(material.getColor(),"Yellow");
              }
    }

从知道我不知道如何实现 materialCons.build();经过设计和分析,我实现了 MaterialCosntructor 如下:

From know i don't how to implemented materialCons.build(); After design and analysis i've implemented MaterialCosntructor as follow:

    public class MaterialConstructor {

         private Helper1 helper1;
         private Helper2 helper2

        MaterialConstructor (Helper1 help1, Helper2 help2){
      .................   
    }

    public Material build(Double among){
         part1=helper1.builpart(among);
        return helper2.construct(part1);
    }

}

在我的初始测试类中,我必须包含这样的代码:

In my initial test class i must includes code like this:

helper1=createMock(Helper1)
helper2=createMock(Helper2)

materialCons=new MaterialConstructor (helper1, helper2)

.....................

expected(helper1.builpart(150)).andReturn(some result)
expected(helper2.construct(some result)).andReturn("Yellow")

结果我们可以得到这个更新的类测试:

as result we can get this updated class test:

  class MaterialTest{
         MaterialConstructor materialCons
        public void testBuild(){
            helper1=createMock(Helper1)
            helper2=createMock(Helper2)
            materialCons=new MaterialConstructor (helper1, helper2)
            expected(helper1.builpart(150)).andReturn(some result)
            expected(helper2.construct(some result)).andReturn("Yellow")
             materialCons.setAmount(150);
             Material material=materialCons.build();
             assertEqual(material.getColor(),"Yellow");

          }
            }

因此我的测试代码将在编写我的源代码后更新!

hence my test code will be update after writen my source code!

这会出现很多次(因为在规范时很难知道必须使用哪个依赖类来解决问题).那么单元测试类在写好源码之后就永远过时了!

This will appear many time ( since it's difficult at specification time to know which dependencies class you must use to solve your problem). Then unit-test class will be always obsolete after written source code!

推荐答案

遵循严格的 TDD 工作流程,例如 TDD 的 3 条法则 绝对会打消你的犹豫.

Following a strict TDD workflow such as the 3 Laws of TDD would definitely put an end to your hesitations.

有了它,你就不能拥有诸如在设计和分析之后我已经实现了 MaterialCosntructor ......"之类的事情,因为你应该

With it you can't have such things as "After design and analysis i've implemented MaterialCosntructor ..." since you are supposed to

只编写足以通过测试的生产代码

Write only enough production code to pass a test

这基本上意味着:只需返回 "Yellow" 作为您的第一个实现.

Which basically means : just return "Yellow" as your first implementation.

然后您将添加更多测试(绿色、红色材料)并逐步设计您的类来处理这些额外的情况.

Then you would add more tests (green, red material) and incrementally design your class to handle these additional cases.

这篇关于当它们是对象依赖时,如何在编写源代码之前编写测试代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆