0

if i have the following method:

function changeObject($object) {
    $object = new \stdClass();
}

This is a very silly example, but it is used to illustrate my question.

The object that is passed may be changed during the method call, but the method does not return the object.

using phpspec how can I assert that the object was modified?

Marty Wallace
  • 34,046
  • 53
  • 137
  • 200

2 Answers2

1

Luckily this is not easy in PhpSpec, nor any other testing framework. Objects are passed by reference in PHP. If you change the reference you no longer have the object which you could compare to. You'd have to copy the original object and compare it based on its properties. Not a good idea.

Method which modifies its arguments is a bad practice in general, a bad idea at least. Usually a client of the method doesn't expect modifications. I'd rather think how to design your class better.

Your example doesn't really describe your problem very well. If you gave a better one, we could provide a better help.

If you only want to verify that a method was called on an object (as opposed to changing the reference from your example), use mocks.

Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125
0

I don't know that there is an effective way to do that without mocking. Check out Phake.

Phake is a mocking framework that allows you to simulate objects in your tests, and verify they call certain methods, or force them to return certain output in your tests.

STLMikey
  • 1,210
  • 7
  • 19