Giter VIP home page Giter VIP logo

Comments (5)

d0x2f avatar d0x2f commented on May 25, 2024 1

Brilliant, that works.
Thank you!

from kahlan.

d0x2f avatar d0x2f commented on May 25, 2024

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

from kahlan.

samsonasik avatar samsonasik commented on May 25, 2024

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

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

from kahlan.

d0x2f avatar d0x2f commented on May 25, 2024

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.

from kahlan.

samsonasik avatar samsonasik commented on May 25, 2024

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

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

from kahlan.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.