【问题标题】:Using Mockery's 'with' expectation to test object equality使用 Mockery 的 'with' 期望来测试对象是否相等
【发布时间】:2014-09-26 09:02:49
【问题描述】:

我正在使用 Mockery 来定义一个期望,即我的 mock 上的函数应该以对象作为参数调用。我在我的测试中声明了预期的对象。问题是测试中的对象与我的代码中的对象通过引用不是同一个对象 - 有没有办法断言两个对象的相等性而不是确切的引用?

测试片段

$resource = new Resource("Test");

$this->aThing
   ->shouldReceive('call')
   ->with($resource)
   ->andReturn(true)
   ->once();

代码片段

public function respondWithString()
{
    // assume $this->aThing is the injected mock

    $resource = new Resource("Test");
    $response = $this->aThing->call($resource);
    return $response;
}

【问题讨论】:

    标签: unit-testing tdd phpunit mockery


    【解决方案1】:

    这应该可行:

    $resource = new Resource("Test");
    
    $this->aThing
       ->shouldReceive('call')
       ->with(Mockery::mustBe($resource))
       ->andReturn(true)
       ->once();
    

    【讨论】:

      猜你喜欢
      • 2011-11-22
      • 1970-01-01
      • 2019-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      相关资源
      最近更新 更多