BenSmith
5 years ago

Testing Eloquent models with PHPSpec

Posted 5 years ago by BenSmith

Hi,

I'm having difficulties trying to test my models using PHPSpec. Here is an example method that I am trying to test:


/**
 * Check if the request has any offers.
 * @return bool
 */
public function hasOffers()
{
    return $this->offered->count() >= 1;
}

And my test:


function it_can_determine_whether_it_has_offers()
{
    $this->getAttribute('offered')->willReturn([]);
    $this->hasOffers()->shouldReturn(false);
}

All of the methods I have on my models are around this level of complexity and should be very straight forward to test, I just can't get PHPSpec set up correctly.

When I run this I get

Fatal error: Call to a member function connection() on null in vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 2948

Any ideas?

Please sign in or create an account to participate in this conversation.