Working with a laravel 5 app and have a problem with phpspec. Why does my Phpspec unit test below fail or more precisely how can I get the stdClass Object keys to match so that it wouldn't fail?
My spec file:
function it_checks_add_starting_date_to_flow()
{
$dealflows = new \stdClass ();
this->add_starting_date_to_flow($dealflows)->shouldReturn((object)[]);
}
And my helper function that I am testing:
public static function add_starting_date_to_flow($dealflows)
{
$dealflows= new \stdClass();
return $dealflows;
}
From phpspec I get the following response:
App/libraries/Mmdealhelpers
65 - it checks add starting date to flow
expected [obj:stdClass], but got [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()
shouldReturn() calls the identity matcher, which uses strict comparison. Your spec is failing as the object you're expecting is not the same instance as the one that's returned from the method.
Use the comparison matcher instead. It uses weak comparison and can is invoked with shouldBeLike():
function it_checks_add_starting_date_to_flow()
{
$dealflows = new \stdClass();
$this->add_starting_date_to_flow($dealflows)->shouldBeLike((object)[]);
}