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 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗