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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭