1

使用 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 我得到以下响应:

App/libraries/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()
4

1 回答 1

2

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

请改用比较匹配器。它使用弱比较并且可以使用shouldBeLike()调用:

function it_checks_add_starting_date_to_flow()
{      
    $dealflows = new \stdClass();
    $this->add_starting_date_to_flow($dealflows)->shouldBeLike((object)[]);
}
于 2015-03-21T20:12:40.317 回答