Coder Social home page Coder Social logo

Comments (7)

jasir avatar jasir commented on August 23, 2024

Works for me, try to add this test:

public function test_issue_4() {
    $this->object->expects('bar')->with(1)->once();
    try {
        $this->object->bar(2);
        $this->object->assertExpectations();
    } catch (\Mockista\MockException $e) {
        $this->assertEquals("Unexpected call in mock unnammed::bar(), args: array (\n  0 => 2,\n)", $e->getMessage());
        return;
    }
    $this->fail('No exception thrown');
}

If you are able to write failing test, I would try to fix that.

from mockista.

frosty22 avatar frosty22 commented on August 23, 2024

So, I'm sorry it is error on my side ... I use your BaseTestCase sample, where Mockista\Registry is used and error is in "$this->mockista->assertExpectations()" call..

This crash - "Mockista\MockException: Expected method unnamed::bar() should be called exactly once but not called at all."

class MockerTest extends \Tester\TestCase
{

    /** @var \Mockista\Registry */
    protected $mockista;

    protected $object;


    protected function setUp()
    {
        $this->mockista = new \Mockista\Registry();
    }

    protected function tearDown()
    {
        $this->mockista->assertExpectations(); // When I comment this, test is correct!
    }


    public function testIssue4() {
        $this->object = $this->mockista->create();
        $this->object->expects('bar')->with(1)->once();
        try {
            $this->object->bar(2);
            $this->object->assertExpectations();
        } catch (\Mockista\MockException $e) {
            Assert::equal("Unexpected call in mock unnammed::bar(), args: array (\n  0 => 2,\n)", $e->getMessage());
            return;
        }
        $this->fail('No exception thrown');
    }


}

from mockista.

frosty22 avatar frosty22 commented on August 23, 2024

So it's really problem in Nette/Tester, I still don't know why and I suppose you don't use it. But in Nette/Tester is problem in TestCase (base class of all tests), in this method of TestCase:

    /**
     * Runs the single test.
     * @return void
     */
    public function runTest($name, array $args = array())
    {
        $this->setUp();
        try {
            call_user_func_array(array($this, $name), $args);
        } catch (\Exception $e) {
        }
        $this->tearDown();
        if (isset($e)) {
            throw $e;
        }
    }

This cause that is returned invalid exception: "Expected method unnamed::bar() should be called exactly once but not called at all."

But when I move calling tearDown method inside try/catch:

    /**
     * Runs the single test.
     * @return void
     */
    public function runTest($name, array $args = array())
    {
        $this->setUp();
        try {
            call_user_func_array(array($this, $name), $args);
            $this->tearDown();
        } catch (\Exception $e) {
        }

        if (isset($e)) {
            throw $e;
        }
    }

It throw correct exception ... it's magic :)

from mockista.

jasir avatar jasir commented on August 23, 2024

It seems that problem is that assertExpectations is called twice - first call "resets" the previous calls, hence this message.

from mockista.

janmarek avatar janmarek commented on August 23, 2024

If you call assertExpectations in tearDown method, you don't have to call it in your tests. It's called automatically for you.

from mockista.

frosty22 avatar frosty22 commented on August 23, 2024

I'm sorry, I placed bad sample code here. When I remove call "assertExpectations" in test's method, it still throw bad exception. For test I dumped mock in assertExpectations and it's really call only once, so there isn't problem. But problem really will be in implements with Nette/Tester.

from mockista.

frosty22 avatar frosty22 commented on August 23, 2024

Heureka! I found error, when method "bar" is called, it throw correct Exception with invalid arguments. But this exception is catch in "runTest" method. After it is call tearDown method and throw Exception with invalid count of calls, which isn't catch.

from mockista.

Related Issues (14)

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.