Testing Private Methods Not Working

霸气de小男生 提交于 2020-01-15 06:12:09

问题


Here is my Test Class;

<?php
namespace stats\Test;

use stats\Baseball;

class BaseballTest extends \PHPUnit_Framework_TestCase
{

  public function setUp() {
    $this->instance = new Baseball();
  }

  public function tearDown() {
    unset($this->instance);
  }

  public function testOps() {
    $obp = .363;
    $slg = .469;
    $ops = $this->instance->calc_ops($obp, $slg); //line 23

    $expectedops = $obp + $slg;

    $this->assertEquals($expectedops, $ops);
  }

}

And this is my Baseball Class;

<?php
namespace stats;

class Baseball
{
  private function calc_ops($slg,$obp)
  {
   return $slg + $obp;
  }
}

And I keep getting this error when I run my tests;

Fatal error: Call to private method stats\Baseball::calc_ops() from context 'stats\Test\BaseballTest' in /media/sf_sandbox/phpunit/stats/Test/BaseballTest.php on line 23

This is only a tutorial I am following.. But it's not working so it's frustrating because I am following it exactly.


回答1:


You can't test private method, you can use a workaround and invoke it via reflection as described in this article.

This is a working example based on the article:

class BaseballTest extends \PHPUnit_Framework_TestCase
{

    public function setUp() {
        $this->instance = new Baseball();
    }

    public function tearDown() {
        unset($this->instance);
    }

    public function testOps() {
        $obp = .363;
        $slg = .469;
//        $ops = $this->instance->calc_ops($obp, $slg); //line 23
        $ops = $this->invokeMethod($this->instance, 'calc_ops', array($obp, $slg));

        $expectedops = $obp + $slg;

        $this->assertEquals($expectedops, $ops);
    }

    /**
     * Call protected/private method of a class.
     *
     * @param object &$object    Instantiated object that we will run method on.
     * @param string $methodName Method name to call
     * @param array  $parameters Array of parameters to pass into method.
     *
     * @return mixed Method return.
     */
    public function invokeMethod(&$object, $methodName, array $parameters = array())
    {
        $reflection = new \ReflectionClass(get_class($object));
        $method = $reflection->getMethod($methodName);
        $method->setAccessible(true);

        return $method->invokeArgs($object, $parameters);
    }



回答2:


Public – The method is publicly available and can be accessed by all subclasses.
Protected – the method / function / property is available to the parent class and all inheriting classes or we call them subclasses or child classes.
Private – the method is private and only available to the parent class / base class.

You can only test private methods within the class and call that public method that using the private method.

class Baseball
{
    public function testMethod()
    {
        $a = 1;
        $b = 2;
        return $this->calc_ops($a, $b);
    }

    private function calc_ops($slg,$obp)
    {
        return $slg + $obp;
    }
}


来源:https://stackoverflow.com/questions/31313492/testing-private-methods-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!