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 第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没法联网
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥100 AT89C52单片机C语言调试之后再回答
  • ¥15 AT89C52单片机C语言串口助手发送数据包返回值
  • ¥15 C++数组中找第二小的数字程序纠错
  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e