5

I'm fairly new to TDD and I'm currently writing tests in a Laravel project and I'm using the Mockery library. I've encountered an issue when trying to mock overload a new class instance.

The problem seems to be that the mocked class loses its inheritance and are therefore not passing the parameter validation when being passed to a method. However when I create a mock of the Child without overloading its inheritance seems to be picked up.

I have to classes called Parent and Child. Child extends Parent.

// Test
public function test() {
    Mockery::mock('overload:Child');
    $results = service();
}

// Functions
public function service() {
    $child = new Child();
    serviceMethod($child);
}

public function serviceMethod(Parent $o) {
    // Do something
}

This returns that serviceMethod expects Parent, not Child.

I'm obviously missing some crucial detail here, would anyone be able to point it out for me?

  • This sounds like expected behaviour. Which one are you having trouble with? What does it give you, what did you expect? – Jonnix Sep 04 '18 at 11:53
  • Thanks for the quick reply, Jon! The thing is that I'm trying to isolate the unit test to only care about the logic in the service() function. So basically I want to prevent triggering the construct of the new Child instance while also controlling the outcome of serviceMethod() with shouldReturn(), if that makes any sense. Please feel free to call me out if my approach to the problem is wrong. – Jonas Hildrestrand Sep 04 '18 at 14:17

1 Answers1

5

I ran into this same issue. We can pass the base class as a second argument. This will fix the type error.

Try this:

Mockery::mock('overload:Child', 'Parent');

or

Mockery::mock('overload:' . Child::class, Parent::class);
crabbly
  • 5,106
  • 1
  • 23
  • 46
  • Thanks for the reply :) I've already finished refactoring the entire project, but I will keep it in mind if I encounter the issue in the future. Cheers! – Jonas Hildrestrand Sep 20 '19 at 11:57