doushan9415 2011-09-06 13:56
浏览 30
已采纳

如何为新的MVC框架创建单元测试

I have been learning MVC recently and have started creating my own framework (for learning purposes only, of course) from http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/

I want to be able to extend these base classes but first I would like to be able to unit test them. Does anyone have an idea how I would go about testing the base Controller, Model and Template classes?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • doufunuo4787 2011-09-06 14:09
    关注

    Just write test classes, preferably mirroring your framework structure (eg ControllerTest, ModelTest, etc), that put the classes through their paces, meaning putting in some data and checking the output. If your code is well-structured, should be quite easy to implement.

    Edit

    Basically, what you test in a unit test is if ( Class::methodToTest( $input ) === $expected_output ). The output must always be identical for the same $input. If this is not the case, or you can't test write a test case like this, it's often an indicator that your code is not well structured (object oriented and loosely coupled).

    Your Template::render() method for example, is not testable because it prints the data instead of returning it. Now, you could work around this by using ob_start(), but better anyway would be to chop the function into smaller parts, that return values instead of directly printing them.

    It's a bit abstract, but I hope you get the point.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改