【问题标题】:Phpspec matching fails with stdClass Objectphpspec 匹配失败,stdClass 对象
【发布时间】:2015-03-21 11:10:37
【问题描述】:

使用 laravel 5 应用程序并遇到 phpspec 问题。为什么我下面的 Phpspec 单元测试会失败,或者更准确地说,我怎样才能让 stdClass 对象键匹配,这样它就不会失败?

我的规范文件:

function it_checks_add_starting_date_to_flow()

    {      
    $dealflows = new \stdClass ();
    this->add_starting_date_to_flow($dealflows)->shouldReturn((object)[]);

    }

还有我正在测试的辅助函数:

public static function add_starting_date_to_flow($dealflows)
    {

    $dealflows= new \stdClass();
    return $dealflows;
    }

从 phpspec 我得到以下响应:

应用程序/库/Mmdealhelpers
65 - 它检查添加开始日期到流 预期 [obj:stdClass],但得到 [obj:stdClass]。

  @@ -1,1 +1,1 @@
  -stdClass Object &000000001d025295000000007dd68060 ()
  +stdClass Object &000000001d02529a000000007dd68060 ()


    80 //                    ]
    81 //                    ));
    82                       $this->add_starting_date_to_flow($dealflows)->shouldReturn((object)[]);
    83 
    84         }
    85 


   0 vendor/phpspec/phpspec/src/PhpSpec/Matcher/IdentityMatcher.php:78
     throw new PhpSpec\Exception\Example\NotEqualException("Expected [obj:stdC...")
   1 [internal]
     spec\App\libraries\MmdealhelpersSpec->it_checks_add_starting_date_to_flow()

【问题讨论】:

    标签: laravel phpspec


    【解决方案1】:

    shouldReturn() 调用identity matcher,它使用严格比较。您的规范失败,因为您期望的对象与从方法返回的对象不同。

    请改用comparison matcher。它使用弱比较并且可以用 shouldBeLike() 调用:

    function it_checks_add_starting_date_to_flow()
    {      
        $dealflows = new \stdClass();
        $this->add_starting_date_to_flow($dealflows)->shouldBeLike((object)[]);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-21
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      相关资源
      最近更新 更多