Laravel-Test-Helpers icon indicating copy to clipboard operation
Laravel-Test-Helpers copied to clipboard

Bug: asserting of relationships does not check returned value

Open laurencei opened this issue 11 years ago • 0 comments

Hi,

I found a 'bug' in AssertHasMany(), AssertHasOne() etc

If I do my relationship like this:

public function photos()
{
   $this->hasMany('Photo');
}

Then AssertHasMany will pass 'green' on phpunit.

public function testProjectHasManyPhotos()
{
    $this->assertHasMany('photos', 'Project');
}

But this is the wrong syntax as I discovered after hours of debugging to work out why my program was not work. It should be;

public function photos()
{
   return $this->hasMany('Photo');
}

So we should make the Asserts ensure the relationship is there AND returned, or it is actually NOT working, and eloquent will fail when you try and use the relationship in your code.

Took me hours to work this out :)

laurencei avatar Jul 02 '13 05:07 laurencei