I'm writing unit tests for my laravel 5 application. In one test, I call a function and need to verify that it the static create
method for another model is called in the function under test.
I don't want to actually persist any resources to the database.
I've created a mock object, specified expectations, and bound it to the app instance, but when create
is called the application attempts to run SQL rather than having the function call intercepted by the mock object.
public function testLogCreation() {
$this->log = Mockery::mock('App\Models\Log');
$this->log->shouldReceive('create')->once();
$this->app->instance('App\Models\Log',$this->log);
echo get_class($this->app['App\Models\Log']); // output: Mockery_2_App_Models_Log
}
There was 1 error
SQLSTATE[23503]: Foreign key violation: 7 ERROR: insert or update on table "logs" violates foreign key constraint....
I also tried $this->log->shouldReceive('save')->once();
since I observed that the static create
function calls the public save
function, but I think I'm not creating the mock expectation on the right instance of log
.
If this can't be accomplished, any suggestions for alternative strategies?
Thanks!