0 Best Reply Awards

  • Member Since 4 Months Ago
  • 21 Lessons Completed
  • 0 Favorites

13th June, 2018

CyclopsSummer started a new conversation Unit Testing: Mocking Model::create() • 4 months ago

I'm writing a method (in a model) that I'm also writing unit tests for. The method calls another model's create() method. In my unit test I want to mock the Model::create() call to avoid side effects in the database, since in this instance, the unit tests are largely for speed.

How can I inject or otherwise control the model dependency? Are models a good use case for a real-time facade?

Another thought is to make a Repository to decouple the model, and mock that via a ServiceProvider, but it seems like a lot of layers for something so simple.

Here's the example:

class User extends Model {
    public function createSandwich(){
        Sandwich::create(['toppings' => ['turkey', 'lettuce', 'tomato']]);

Objective: unit test User::createSandwich() mocking Sandwich to make sure its create method is called.

Edit Your Profile

Want to change your profile photo? We pull from