【问题标题】:How do I mock a method called within the constructor in a PHP unit test?如何在 PHP 单元测试中模拟构造函数中调用的方法?
【发布时间】:2014-05-29 12:33:13
【问题描述】:

我在对具有在构造函数中调用的方法的类进行单元测试时遇到问题。我不明白如何嘲笑这个。也许我应该使用 phpUnit 的 'setUp' 方法?

我正在使用 Mockery 库。还有比这更好的工具吗?

class ToTest
{

   function __construct() {

       $this->methodToMock(); // need to mock that for future tests 

   }

   // my methods class

}

任何建议将不胜感激。

【问题讨论】:

    标签: php unit-testing mocking phpunit mockery


    【解决方案1】:

    只要扩展这个类并覆盖你的方法,如果它是公共的或受保护的。

    【讨论】:

      【解决方案2】:

      这里的问题是该方法不能被模拟,因为对象还没有被实例化。 sectus 答案是有效的,但可能不是很灵活,因为在不同的测试中改变模拟方法的行为可能很困难。

      您可以创建另一个与您要模拟的方法相同的类,并将该类的实例作为构造函数参数传递。这样你就可以在考试中通过模拟课程。通常你遇到的问题是班级做太多事情的味道。

      【讨论】:

      • 感谢您的回答!真的很感激,你可以做一个简单的例子吗?谢谢:)
      【解决方案3】:

      要测试这个类,您将模拟内部对象 (methodToMock),然后使用依赖注入来传递模拟的服务而不是真实的服务。

      类:

      class ToTest{
          private $svc;
      
          // Constructor Injection, pass the Service object here
          public function __construct($Service = NULL)
          {
              if(! is_null($Service) )
              {
                  if($Service instanceof YourService)
                  {
                      $this->SetService($Service);
                  }
              }
          }
      
          function SetService(YourService $Service)
          {
              $this->svc = $Service
          }
      
          function DoSomething($request) {
              $svc    = $this->svc;
              $result = $svc->getResult($request);        // Get Result from Real Service
              return $result;
          }
      
          function DoSomethingElse($Input) {
               // do stuff
               return $Input;
          }
      }
      

      测试:

      class ServiceTest extends PHPUnit_Framework_TestCase
      {
          // Simple test for DoSomethingElse to work Properly
          // Could also use dataProvider to send different returnValues, and then check with Asserts.
          public function testDoSomethingElse()
          {
              $TestClass = new YourService();
              $this->assertEquals(1, $TestClass->DoSomethingElse(1));
              $this->assertEquals(2, $TestClass->DoSomethingElse(2));
          }
      
          public function testDoSomething()
          {
              // Create a mock for the YourService class,
              // only mock the DoSomething() method. Calling DoSomethingElse() will not be processed
              $MockService = $this->getMock('YourService', array('DoSomething'));
      
              // Set up the expectation for the DoSomething() method 
              $MockService->expects($this->any())
                          ->method('getResult')
                          ->will($this->returnValue('One'));
      
              // Create Test Object - Pass our Mock as the service
              $TestClass = new ToTest($MockService);
              // Or
              // $TestClass = new ToTest();
              // $TestClass->SetService($MockService);
      
              // Test DoSomething
              $RequestString = 'Some String since we did not specify it to the Mock';  // Could be checked with the Mock functions
              $this->assertEquals('One', $TestClass->DoSomething($RequestString));
          }
      }
      

      【讨论】:

      • 漂亮干净的解释!谢谢!我会试试的,我会告诉你怎么回事!那么接受的答案将是你的:)
      • 没问题,希望对您有所帮助。
      【解决方案4】:

      如果你的类难以实例化以进行测试,那是你的类做太多或在构造函数中工作的代码味道。

      http://misko.hevery.com/code-reviewers-guide/

      Flaw #1: Constructor does Real Work

      警告标志

      • 构造函数或字段声明中的新关键字
      • 在构造函数或字段声明中调用静态方法
      • 除了构造函数中的字段赋值之外的任何东西
      • 构造函数完成后对象未完全初始化(注意 用于初始化方法)
      • 构造函数中的控制流(条件或循环逻辑)
      • 代码在构造函数中执行复杂的对象图构造 而不是使用工厂或建筑商
      • 添加或使用初始化块

      无论您的 methodToMock 函数在构造函数中做什么,都需要重新考虑。正如其他答案中提到的,您可能希望使用依赖注入来传递您的班级正在做的事情。

      重新思考你的类实际上在做什么并重构,以便更容易测试。这还具有使您的类更容易在以后重用和修改的好处。

      【讨论】:

        【解决方案5】:

        我也想知道这就是我找到您的问题的方式。最后我决定做一些有点脏的事情......使用反射。

        这是我要测试的方法:

        /**
         * ArrayPool constructor.
         * @param array $tasks Things that might be tasks
         */
        public function __construct(array $tasks)
        {
            foreach ($tasks as $name => $parameters) {
                if ($parameters instanceof TaskInterface) {
                    $this->addTask($parameters);
                    continue;
                }
                if ($parameters instanceof DescriptionInterface) {
                    $this->addTask(new Task($parameters));
                    continue;
                }
                $this->addPotentialTask($name, $parameters);
            }
        }
        

        出于本次测试的目的,我不想实际运行->addTask 或->addPotentialTask,只知道它们会被调用。

        这是测试:

        /**
         * @test
         * @covers ::__construct
         * @uses \Foundry\Masonry\Core\Task::__construct
         */
        public function testConstruct()
        {
            $task = $this->getMockForAbstractClass(TaskInterface::class);
            $description = $this->getMockForAbstractClass(DescriptionInterface::class);
            $namedTask = 'someTask';
            $parameters = [];
        
            $arrayPool =
                $this
                    ->getMockBuilder(ArrayPool::class)
                    ->disableOriginalConstructor()
                    ->setMethods(['addTask', 'addPotentialTask'])
                    ->getMock();
        
            $arrayPool
                ->expects($this->at(0))
                ->method('addTask')
                ->with($task);
            $arrayPool
                ->expects($this->at(1))
                ->method('addTask')
                ->with($this->isInstanceOf(TaskInterface::class));
            $arrayPool
                ->expects($this->at(2))
                ->method('addPotentialTask')
                ->with($namedTask, $parameters);
        
            $construct = $this->getObjectMethod($arrayPool, '__construct');
            $construct([
                0=>$task,
                1=>$description,
                $namedTask => $parameters
            ]);
        }
        

        魔术发生在getObjectMethod 中,它接受一个对象并返回一个可调用闭包,该闭包将调用该对象实例上的方法:

        /**
         * Gets returns a proxy for any method of an object, regardless of scope
         * @param object $object Any object
         * @param string $methodName The name of the method you want to proxy
         * @return \Closure
         */
        protected function getObjectMethod($object, $methodName)
        {
            if (!is_object($object)) {
                throw new \InvalidArgumentException('Can not get method of non object');
            }
            $reflectionMethod = new \ReflectionMethod($object, $methodName);
            $reflectionMethod->setAccessible(true);
            return function () use ($object, $reflectionMethod) {
                return $reflectionMethod->invokeArgs($object, func_get_args());
            };
        }
        

        而且我知道循环和条件都可以正常运行,而无需进入我不想在此处输入的代码。

        TL;DR:

        1. 禁用__construct
        2. 设置模拟
        3. 对象实例化后使用反射调用__construct
        4. 尽量不要为此失眠

        【讨论】:

          【解决方案6】:
          class ToTest
          {
             function __construct(){
                 $this->methodToMock(); // need to mock that for future tests 
             }
             // my methods class
              public function methodToMock(){}
          }
          
          class ToTestTest{
              /**
               * @test
               * it should do something
               */
              public function it_should_do_something(){
                  $ToTest = \Mockery::mock('ToTest')
                  ->shouldDeferMissing()
                  ->shouldReceive("methodToMock")
                  ->andReturn("someStub")
                  ->getMock();
          
                  $this->assertEquals($expectation, $ToTest->methodToMock());
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2021-09-29
            • 1970-01-01
            • 2010-11-17
            • 1970-01-01
            • 2013-12-26
            • 2018-09-14
            • 1970-01-01
            • 1970-01-01
            • 2015-09-18
            相关资源
            最近更新 更多