【问题标题】:PHPUnit prophesize a method without exact argumentsPHPUnit 预言一个没有确切参数的方法
【发布时间】:2016-06-22 21:54:44
【问题描述】:

我正在使用 prophecy 模拟 UserRepository 类,以确保在向 /user 发送 POST 请求时,会触发 UserRepository 上的 create() 方法。

$repository = $this->prophesize(UserRepository::class);

$repository->create()->shouldBeCalled()

唯一的问题是create() 方法将请求数据作为参数发送到存储库,以便在执行任何操作之前对输入进行一些认真的调整。我如何模拟create() 调用而不告诉预言参数是什么?

或者这对我来说是一种非常糟糕的做法,并且永远不应该将请求数据传递到存储库?

【问题讨论】:

    标签: php laravel testing mocking prophecy


    【解决方案1】:
    use Prophecy\Argument;
    
    $repository->create(Argument::any())->shouldBeCalled()
    

    【讨论】:

      【解决方案2】:
      use Prophecy\Argument;
      
      $repository->create(Argument::cetera())->shouldBeCalled()
      

      any() 匹配任何单个 值,其中cetera所有 值匹配到签名的其余部分。

      【讨论】:

        猜你喜欢
        • 2014-05-05
        • 2013-09-15
        • 1970-01-01
        • 2012-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-16
        相关资源
        最近更新 更多