Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shaddowdew11's avatar

Issues with Mocking an Eloquent Model

I've looked through literally hundreds of pages, and I can't seem to find what makes my situation different than the working examples I've found on these pages.

I'm using Laravel 4.2, PHPUnit 4.3.1, and latest Mockery from Composer. I'm running a PHPUnit test on my Controller, AppController, and I'd like to intercept an Eloquent database save() by mocking the model. While there are no errors thrown when I create the Mock, the mock also isn't attaching to my model, so the rows are still being created.

What am I missing here? Thanks!

My error from Mockery: Mockery\Exception\InvalidCountException: Method save() from Mockery_0_EventRsvp should be called exactly 1 times but called 0 times.

// Located at /app/tests/app/AppControllerTest.php
class AppControllerTest extends TestCase {
    public function setUp() {
        parent::setUp();
        Session::start();
        Mail::pretend();
    }
    public function tearDown() {
        parent::tearDown();
        \Mockery::close();
    } 
    public function testPostApp() {
        $myvar = array();
        $this->mock = \Mockery::mock('Eloquent','EventRsvp');
        $this->app->instance('EventRsvp', $this->mock);
        $this->mock
                ->shouldReceive('save')
                ->once()
                ->andReturn('true');
        $response = $this->call('POST', '/3tDYSL0', $myvar);
    }
}
// Located at /app/controllers/AppController
class AppController extends BaseController {
    public function saveApp($shortUrl){
        $rsvp = new EventRsvp;
        $rsvp->fieldone = '124';
        $rsvp->fieldtwo = '30233';
        $rsvp->save();

        $returnredirect = Redirect::to(Request::path(). '/complete');
        return $returnredirect;
    }
}
// Located at /app/models/EventRsvp.php
<?
class EventRsvp extends Eloquent {

    protected $guarded = array('id');
    use Illuminate\Database\Eloquent\SoftDeletingTrait;
    protected $dates = ['deleted_at'];

    public function relationshipone()
    {
        return $this->belongsTo('RelationshipOne','idone');
    }

    public function relationshiptwo()
    {
        return $this->belongsTo('RelationshipTwo','idtwo');
    }

}
?>
0 likes
4 replies
shaddowdew11's avatar

@psmail Thanks for the response Paul, but unfortunately the error you had in the linked thread isn't quite my issue.

psmail's avatar

My apologies for misreading.

I forwarded the link because 'Mockery_0_EventRsvp ...' suggest a full path that is required and is not there. And indeed in your code, you've not used a full path to your object.

Did you try a full path on your object? Did it change the outcome / error message?

shaddowdew11's avatar

@psmail Aye, I've indeed tried a full path, with no change in error message. I've tried '/app/models/EventRsvp' and 'app/models/EventRsvp' with no change in the error message: Mockery\Exception\InvalidCountException: Method save() from Mockery_0_EventRsvp should be called exactly 1 times but called 0 times.

Please or to participate in this conversation.