编写“可单元测试"代码? [英] Writing "unit testable" code?

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

问题描述

您使用什么样的实践来使您的代码对单元测试更加友好?

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

推荐答案

  • TDD——先写测试,强制你要考虑可测试性和帮助编写实际上是的代码需要,而不是你认为你可能的需要

    • 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

      避免密封类

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

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