dt614037527 2016-09-21 14:01
浏览 36
已采纳

symfony中的单元测试实体何时使用测试db?

I know that you can test your models by specifying a temporary test database to be used by your test framework. Is this something that should be used always when testing a model? Or testing the functionality of just the class without persisting anything to the database is ok?

  • 写回答

1条回答 默认 最新

  • drvkf88226 2016-09-21 14:23
    关注

    It's really too general to say, if your model contains data manipulation logic, then it's not necessary to test against a real db. The logic can be validated by configuring fake data, calling the manipulation method and verifying the output. Your model must be designed in a way that supports this, though. If the model method is responsible for pulling data from the db, manipulating the data, and then persisting data it needs to be refactored to isolate those 3 distinct operations.

    Testing that your code works with an actual specific version of a database is necessary too, but most of your application logic should be able to validated at the unit test level.

    enter image description here

    Mike Cohn's test pyramid visually describes this concept. The majority of automated validation should occur at the lowest level possible. Integration tests (your app to mysql) are necessary but should only cover the actual integration and not excessively exercise application logic. This is because integration tests are more difficult to maintain and are slower because they use external resources. If you test on a shared environment (jenkins) then there are often operational issues in managing the resources.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集