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 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂