|
|
|
|
|
by wvenable
4899 days ago
|
|
Obviously, it would make sense to pass an instance of the object as the first argument to every callback. A mocked class stands in for the original class. All the methods are stubs. Sometimes you need to provide functionality for the stub methods. Isn't it most logical to use a function to provide a stub method's body? It's my opinion that all the stuff PHPUnit unit does with mocks (and JUnit as it's based on) is because Java didn't have anonymous functions. So you had to call methods to essentially construct code for the stub method body. But if you can provide actual code than all that API is unnecessary. |
|
This is purposeful. The point of passing the original mime object in two-fold. It is first that it allows you to manipulate the object further when and only when the method is called. This is extremely useful for mocking/stubbing very dynamic objects whose behavior may change based on a method call. Secondly, it allows you to return the mimicked object.
Think of a method, for example that might expect two arguments, set a number of properties, and then return itself. Now imagine it has a second method, which may also alter one of those properties and return something else completely. This is pretty straightforward to do with parody:
I am not readily familiar with the syntax of other frameworks... but this seems a lot more flexible than passing arguments to the callback and forcing untested logic into the callbacks.As I mentioned in another comment -- this is designed for very strict and context sensitive test cases. In principle, everything you should ever expect when calling a method is to give something and get something back, and maybe that it modifies a property. All of this is possible here, the logic of how it transforms what you get to what it gives is irrelevant, you should know the answer to both as a developer. And by removing that logic from the code, you "fake" only the very explicit expectations of the code you're pretending to be.