Skip to content
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

Unable to stub without overriding function #401

Closed
d0x2f opened this issue Oct 27, 2022 · 5 comments
Closed

Unable to stub without overriding function #401

d0x2f opened this issue Oct 27, 2022 · 5 comments
Labels

Comments

@d0x2f
Copy link

d0x2f commented Oct 27, 2022

The online documentation states

Note: When andReturn()/andRun() is not applied, toReceive() simply act as a "spy" and let the code execution flow to be unchanged.

But I can't quite get this to work as I expect, I've made a minimal reproduction repo here: d0x2f/kahlan-stub-repro.

To be clear, what I'm trying to do is spy on a function and check that it was called n times without changing the behaviour of the function.

Thanks for your help!

@d0x2f
Copy link
Author

d0x2f commented Oct 27, 2022

Relevant snippets:

    class MyClass
    {
        private function getString(): string
        {
            return "the string";
        }

        public function useString(): string
        {
            return $this->getString();
        }
    }
    describe('without explicit return value', function () {
        beforeEach(function () {
            allow(MyClass::class)->toReceive("getString");
            $myClass = new MyClass();
            $this->result = $myClass->useString();
        });

        it('returns the string', function () {
            expect(MyClass::class)->toReceive("getString")->once();
        });

        it('returns the string', function () {
            expect($this->result)->toBe("the string");
        });
    });
MyClass->getString()
  without explicit return value
    ✖ it returns the string
      an uncaught exception has been thrown in `src/MyClass.php` line 7
      
      message:`TypeError` Code(0) with message "KahlanStub\\MyClass::getString(): Return value must be of type string, null returned"
      
        [NA] - src/MyClass.php, line  to 7
        KahlanStub\MyClass::getString() - src/MyClass.php, line 13
        KahlanStub\MyClass::useString() - spec/MyClass.spec.php, line 10

@samsonasik
Copy link
Collaborator

you need to set ->andReturn('the string')

-            allow(MyClass::class)->toReceive("getString");
+            allow(MyClass::class)->toReceive("getString")->andReturn('the string');

@d0x2f
Copy link
Author

d0x2f commented Oct 27, 2022

Thanks @samsonasik, but this was just a minimal example to demonstrate that I can't stub a function without overriding it.
In reality the function is much too complicated to reimplement in the tests using andReturn or andRun.

I want to just spy on the function and assert that it was called, as the docs say is possible.

@samsonasik
Copy link
Collaborator

I see, in that case, you need to use expect() instead of allow():

-            allow(MyClass::class)->toReceive("getString");
+            expect(MyClass::class)->toReceive("getString");

@d0x2f
Copy link
Author

d0x2f commented Oct 27, 2022

Brilliant, that works.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants