Codeception: Bug: Stub method invoke count test not working

Created on 15 Jul 2013  路  27Comments  路  Source: Codeception/Codeception

This test should fail because the method is called only one time but is expected to be called two times. However the test won't fail in current stable version (I didn't try dev).

Similarly Stub::once(...) won't fail if never called.

<?php

use Codeception\Util\Stub;

class Foo
{
    public function method() {}
}

class ExampleTest extends \Codeception\TestCase\Test
{

    public function testSomething()
    {
        $foo = Stub::makeEmpty('Foo', array(
            'method' => Stub::exactly(2, function () {}),
        ));

        $foo->method();
    }

}

Most helpful comment

To make verification working automatically you have to pass an instance of PhpUnit_Framework_TestCase as the last argument to Stub::makeX() method.
$this works in all test formats in Codeception 2.1.

class ExampleTest extends \Codeception\TestCase\Test
{

    public function testSomething()
    {
        $foo = Stub::makeEmpty('Foo', array(
            'method' => Stub::exactly(2, function () {}),
        ), $this);

        $foo->method();
    }

}

All 27 comments

I think the bug exists in dev too.
Sorry, was working on a different parts, and didn't have opportunity to look into Stub class.
If you can discover what actually happens there, that would be awesome.
Meanwhile, I'm not sure when I can take a look on this.

Sorry but I've already switched to Mockery...

:) Yep, I plan to switch to something too.
The stub classes work well, but I'm pretty unsure how mocking works in this case.
Mocking capabilities were developed by @svsool

@svsool I don't understand (nor do I care anymore). I've just used it as described in the annotation here. So maybe the annotation is wrong...

@enumag If you have successfully moved to Mockery, so could you share your experience?
https://twitter.com/farconadaT3/status/357873607122231296

If you could make a protip on CoderWall or gist or blogpost about it, etc, that would help many others.

@DavertMik Well I just use Mockery::mock('...') according to the documentation and call Mockery::close(); in the _after() method. It's very simple really.

Hello, I try to use Stub::once() and other related methods, but noticed too that they never fail. Same as @enumag described in top example. I'm using version v1.8.1.
Is here any tips how to fix it or, perhaps, i have to try another version?

can you upgrade to latest 1.9 version and check if this error still exists?

Hm, but it looks like here no 1.9 version...
I'm looking here:
https://github.com/Codeception/Codeception/

Where can I get latest 1.9?

Install/update it with composer, then run vendor/bin/codecept --version and tell what version it is.
For example for me just installed codeception:
vendor\bin\codecept --version Codeception version 1.9-dev

it return "Codeception version 1.8.1"

cant be so, whats steps you were taken to install it? if you already have it installed then run composer update.

Done. I run "php composer.phar update"
Here is output:

Loading composer repositories with package information
Updating dependencies (including require-dev)

  • Removing phpunit/phpunit (3.7.28)
  • Installing phpunit/phpunit (3.7.29)
    Loading from cache

Generating autoload files

Also tested Codeception version. It is still 1.8.1

hm, not sure if i can help anymore in this case, maybe you can ask for @DavertMik help i guess. Or use mockery as suggested above, because it has a lot of features and designed espesially for this case.

ok, thank you!
I will try ask here - now sure how better to contact..
@DavertMik if you can help with that issue, answer, please.

There is no dev branch for codeception 1.9 anymore because it was renamed to 2.0. Try this in your composer.json: "codeception/codeception": "dev-2.0-dev".

As for Mockery, it would be probably good idea to include this simple module to Codeception https://github.com/arachne/Codeception/blob/master/src/Codeception/Module/Mockery.php

Yep. Module is really simple and I'd really like to include it, but please can you make a separate composer's package for that?

Also could you add some documentation for this class. Just a few words: what is mockery, where is its docs, and maybe some examples. Nothing more.

We will include this module into Codeception 2.0 phar then.

Thank you for help!

Yes, I will try Mockery as variant then.

@DavertMik Ok, I'll send a PR. Separate composer package is not needed in my opinion - for previous versions than 2.0 you can just use my arachne/codeception package. There is also a module for Nette framework but you don't have to use it. As for the Nette module, I don't think it is stable enough to put it into Codeception itself.

I think I will close this as there is Mockery module
https://github.com/Codeception/MockeryModule

And Stub class should handle only stubbing methods... Mocking is pretty complicated

@DavertMik Just asking but why not a extension?

Extensions are more general and more powerful then modules.
Also Extensions are enabled for all suites, while Mockery is required only for unit.
Basically module is the right place for Mockery. Also not too many people are aware of extensions, so it will be harder for them to use it )

As for me, Mockery is too verbose. Especially when comparing with Stub. So I wait for this to be fixed.

Agrh, why no one told me I need to call $mock->__phpunit_verify(); manually? Should this be in the docs, @DavertMik, shouldn't it? Or maybe there is a better way to do this check automatically.

@neoascetic I believe that the fact that you need to call it manually is a bug. It would be better if Codeception called it by itself. Anyway I've switched to Mockery long time ago.

To make verification working automatically you have to pass an instance of PhpUnit_Framework_TestCase as the last argument to Stub::makeX() method.
$this works in all test formats in Codeception 2.1.

class ExampleTest extends \Codeception\TestCase\Test
{

    public function testSomething()
    {
        $foo = Stub::makeEmpty('Foo', array(
            'method' => Stub::exactly(2, function () {}),
        ), $this);

        $foo->method();
    }

}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Breadleyg picture Breadleyg  路  3Comments

sebastianneubert picture sebastianneubert  路  3Comments

nikolaykolev picture nikolaykolev  路  3Comments

rogoit picture rogoit  路  3Comments

centerax picture centerax  路  4Comments