duanniwu7730 2017-03-17 09:41
浏览 5

模拟PHPUnit中的所有有效方法

I have following problem:
I want to test controller if no exception is rised during dispatch, but controller uses external library which has own phpunit tests. This library also use external library with own tests and so on...

So my application looks similar to this diagram:

request -> router -> dispatch -> controller -> view

If any error occures during this procces. Application throws exception. I want to test controller, but for now I have to write +50 lines of code to mock all object needed.

Image for example that I pass form from controller to view.
In view I have something like this:

<?php echo $form->openTag('form'); ?>
    <?php echo $form->openTag('fieldset'); ?>
        <?php echo $form->label('firstName'); ?>
        <?php echo $form->input('firstName'); ?>
        <?php echo $form->errorMessages('firstName'); ?>
   <?php echo $form->closeTag('fieldset'); ?>
   <?php echo $form->openTag('fieldset'); ?>
        <?php echo $form->label('gender'); ?>
        <?php echo $form->radio('gender'); ?>
        <?php echo $form->errorMessages('gender'); ?>
   <?php echo $form->closeTag('gender'); ?>
   ...
<?php echo $form->closeTag('form'); ?>

Let's say I have form with 20 inputs... Do you get my point? I have to mock all available methods for this form to pass my test.

It is possible to mock object with all valid methods?

I mean if there is a way to mock class which will have all methods valid? Without need to define anything.
If application will need object of class A it will return object of class A.
If application will need array it will return array
etc..

  • 写回答

1条回答 默认 最新

  • dpx49470 2017-03-17 09:51
    关注

    We know very little of your application context, but by default $this->createMock(Some::class) would create a mock of the entire class - no need to mock every method. Then you can define behavior only to certain methods and that's the entire point of mocking. So in your case it would be your form class I guess, and you will have to set it to the view.

    But I would ask what are you testing? If you are testing if the form would throw an exception, you wouldn't want to mock it, but execute it and check for exception.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度