doufei4418 2014-12-19 17:18
浏览 81
已采纳

使用Laravel,PHPSpec和PHPUnit测试模型和服务

Im having difficulty deciding or understanding the best approach to testing in Laravel.

I really like the behavioural side of PHPSpec testing, although it is just not compatible with testing Eloquent models or anything related to an active record ORM.

When testing things like service providers PHPSpec seems like the way to go.

** Is it desirable to test models with something like PHPUnit, then test other non ORM layers such as service providers with something like PHPSpec? ***

  • 写回答

2条回答 默认 最新

  • dongnaopa6200 2014-12-21 10:54
    关注

    I don't know Laravel, but reading problems people have with unit testing it tells me there's a lot of design issues in there. I understood that those problematic features of Laravel are optional, so there's hope!

    See my comparision of PhpSpec vs PhpUnit in an answer of another question: First Shot at Testing Laravel 4 apps (PHPSpec/BDD vs. PHPUnit/TDD)

    I'm guessing you'll face lots of problems using PhpSpec with Laravel, and often you'll have to avoid specing Laravel specific quirks, or avoiding them (facades are optional, you can use proper dependency injection). I'd aim on having as much code as possible independent from the framework and properly unit test them. Then, you can have a thin layer of framework glue code, covered with functional or integration tests. Apply the dependency injection.

    As an example taken from the land of Symfony and Doctrine, I usually don't write specs for Doctrine repositories. Well, I only expect they implement a certain interface. That's all. The rest is covered with my acceptance tests. There is no much value in verifying that repositories use query builder to produce an expected query. No point really.

    Going back to the tools, I can see myself mixing different kind of testing tools for different kinds of testing:

    • phpspec - unit tests - I'll write most of the tests on this level
    • behat - acceptance tests
    • phpunit - integration and functional tests - I'll have a small number of these, as they're fragile and slow.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.