duanpin2009 2015-08-27 12:06
浏览 143
已采纳

PHP Mockery:如何设置方法或另一种方法的期望?

I'm trying to use Mockery and PHPUnit to test if one method OR another method of a given class will be called when some piece of code is executed.

This is exactly what I'm trying to do (I'm using Laravel 5):

// Test an email is sent to new user
// upon account creation.
public function testEmailSentOnNewUserCreation()
{
    // Mail can be sent right on ...
    Mail::shouldReceive('send')->once(); //...OR...
    // ... it can be queued for later.
    Mail::shouldReceive('queue')->once();
    // I'm ignoring parameters and returning values here
    // for sake of brevity.

    $u = new User;
    $u->name = 'Jon Doe';
    $u->email = 'jon@doe.com';
    $u->save();
}

So, my question is about how to implement the OR part, if it's possible. I also searched for some PHPUnit annotation that could be helpful, but I couldn't find anything.

  • 写回答

1条回答 默认 最新

  • douduiti3040 2015-08-31 16:43
    关注

    One of the paradigms of unit testing is that you should be in control of the exact flow for each test case you implement. That's why no if, switch, etc should appear in your test.

    Whenever the object under test behaviour depends on another object, or on a given condition (which logically is more or less the same), the dependency must be mocked in order to take control of the case you're testing.

    So, in your case, you should mock the dependency that makes your class invoke the send or queue methods.

    I think that's why testing frameworks do not implement what you intended to use.

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看