dt614037527
dt614037527
2016-09-21 14:01

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 drvkf88226 5年前

    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.

    点赞 评论 复制链接分享