写作与QUOT;单元测试" code? [英] Writing "unit testable" code?

查看:411
本文介绍了写作与QUOT;单元测试" code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你用什么样的做法,以使您的code的单元测试友好吗?

What kind of practices do you use to make your code more unit testing friendly?

推荐答案


  • TDD - 先写测试,力量
    你想想可测试性和
    帮助写code,实际上是
    需要的,不是你认为你可能
    需要

  • TDD -- write the tests first, forces you to think about testability and helps write the code that is actually needed, not what you think you may need

    重构到接口 - 品牌
    嘲讽容易

    Refactoring to interfaces -- makes mocking easier

    公共虚拟方法
    接口 - 使嘲弄更容易

    Public methods virtual if not using interfaces -- makes mocking easier

    依赖注入 - 使得嘲讽
    容易

    Dependency injection -- makes mocking easier

    更​​小,更有针对性的方法 -
    测试是更集中,更容易

    Smaller, more targeted methods -- tests are more focused, easier to write

    静态类的规避

    避免单身,除非
    必要

    Avoid singletons, except where necessary

    避免密封类

    这篇关于写作与QUOT;单元测试" code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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