lundi 31 août 2015

Stubed object is calling real methods in codeception

I'm trying to run tests using codeception and his library for Mock and Stub. what's happening is that when i'm trying to run my test with my Stubs, it's raising an error that is implemented in my real classes.

I think that a Stub is way to isolate the test and emulate real objects. Why it's calling my real methods?

<?php

class FirstDegreeTest extends \Codeception\TestCase\Test
{
  /**
   * @var \UnitTester
   */
  protected $tester;

  protected function _before()
  {
    $this->max_threads = 30;
    $this->firstDegree = new AlThread\LoadControl\Measurer\FirstDegree($this->max_threads);

    $sensor = Stub::make("AlThread\LoadControl\Sensor\LoadAvg", array("getSystemLoad", Stub::consecutive(0, 1, -3, 1.1, null)));

    $this->firstDegree->setSensor($sensor);
  }

  public function testMeasure()
  {    
    $this->assertEquals(30, $this->firstDegree->measure());
  }
}
?>

So when I run the codeception (php codeception.phar run):

Time: 487 ms, Memory: 11.25Mb

There was 1 error:

---------
1) Test me (FirstDegreeTest::testMeasure)

  [PHPUnit_Framework_Exception] rewind() expects parameter 1 to be resource, null given
                                                                                                 #1  Codeception\Subscriber\ErrorHandler->errorHandler
#2  /home/AlThread/src/LoadControl/Sensor/LoadAVG.php:42
#3  /home/AlThread/src/LoadControl/Sensor/LoadAVG.php:55
#4  /home/AlThread/src/LoadControl/Sensor/LoadAVG.php:49
#5  /home/AlThread/src/LoadControl/Sensor/LoadAVG.php:61
#6  /home/AlThread/src/LoadControl/Sensor/LoadAVG.php:67
#7  /home/AlThread/src/LoadControl/Sensor/LoadSensor.php:10
#8  /home/AlThread/src/LoadControl/Measurer/LoadMeasurer.php:32
#9  /home/AlThread/tests/unit/FirstDegreeTest.php:31
#10 FirstDegreeTest->testMeasure

FAILURES!
Tests: 3, Assertions: 2, Errors: 1.

So here is the problem:

[PHPUnit_Framework_Exception] rewind() expects parameter 1 to be resource, null given

The traceback take me until the real implementation of class LoadAVG, where i'm realy calling the rewind() function, and obviously, raise an error, once this class doesn't have the appropriate environment to run.

Will be that I'm thinking about Stubs in the wrong way?

Thank you very much.

Aucun commentaire:

Enregistrer un commentaire