-
Notifications
You must be signed in to change notification settings - Fork 241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to allow call to original method? #332
Comments
Partial mocking is something we explicitly decided to never support in Prophecy, as needing them is generally a sign that your class violates SRP. And one of the goal of Prophecy (and of PhpSpec in general) is to push you away from bad architectures. If you cannot refactor your code, use a different mocking library for this case (the old PHPUnit_MockObject system supports partial mocks, and is still available in PHPUnit in parallel of Prophecy) |
It may be helpful to add something about this a bit more explicitly to the documentation. Perhaps define and describe partial mocks, and why they're unsupported. This may save users like me hours picking through the code trying to untangle how to make prophecy spit out a partial double. Based on my description, does the injected wrapper sound like a reasonable design? I generally avoid adding code paths in production code exclusively used by test code, but unless I force the production caller to always inject the wrapper object, (or forcing them to use DI container) injecting the wrapper class at run time will only be used by test code. Normally, I'd override the accessor to the wrapper object with the mocked object in a partial double. What do you recommend as an alternative? |
(I suppose I could simply make the wrapper object property public, and set it to the mock in my test code) |
you can make the wrapper an optional constructor argument (instantiating one automatically in the constructor if not provided) |
Yes, but that still makes not constructing it when provided a testing-specific code path. :-( |
I just ran into the same issue, and while I suspected that the ultimate solution was to rearchitect the affected class, I would have really appreciated seeing this called out in the docs. |
Fix #332: Calling original method is unsupported
I suspect that I'm running into this issue as I'm trying to mix a wrapper that should probably be injected into the class that's using it, but I distinctly remember mocking some methods and not others in PHPUnit's mocking system some years ago, and would like to know how to do this in Prophecy.
In this situation, I have a class with four methods:
How do I write tests for the last method, with the first two or three methods mocked. I tried using
$objProph->methodToTest('argValue')->shouldBeCalled();
, but thereveal()
ed object's method always returnedNULL
and I suspect never called the parent class method.I am aware that I can simply put the first two methods in their own class (and perhaps I should do that) and pass this object to the original class's constructor. Then I could easily mock the injected object without worrying about mocking for the other two methods, but this seems a bit cumbersome.
Is there currently a way in Prophecy to mock some methods and not others in a class? I thought these hybridized object classes were called "test doubles" but I may have my terminology wrong.
Please advise, Thanks.
The text was updated successfully, but these errors were encountered: