模拟由新类对象调用的方法的单元测试


Unit test for mocking a method called by new class object

我正在为现有代码编写单元测试,如下所示

class someClass {
    public function __construct() { ... }
    public function someFoo($var) {
        ...
        $var = "something";
        ...
        $model = new someClass();
        model->someOtherFoo($var);
    }
    public someOtherFoo($var){
         // some code which has to be mocked
    }
}

在这里,我应该如何能够模拟函数"someOtherFoo"的调用,这样它就不会在someOtherFoo中执行"some code"?

class someClassTest {
   public function someFoo() {
      $fixture = $this->getMock('someClass ', array('someOtherFoo'));
      $var = "something";
      ....
      // How to mock the call to someOtherFoo() here
   }
}

是否可以模拟构造函数,以便它返回我自己构造的函数或变量?

谢谢

只要在测试的方法中有new XXX(...),您就注定要失败。将实例化提取到同一个类的新方法——createSomeClass(...)。这允许您创建被测类的部分模拟,该模拟从新方法返回一个存根值或模拟值。

class someClass {
    public function someFoo($var) {
        $model = $this->createSomeClass();  // call method instead of using new
        model->someOtherFoo($var);
    }
    public function createSomeClass() {  // now you can mock this method in the test
        return new someClass();
    }
    public function someOtherFoo($var){
         // some code which has to be mocked
    }
}

在测试中,在调用someFoo()的实例中模拟createSomeClass(),在从第一个模拟调用返回的实例中模拟someOtherFoo()

function testSomeFoo() {
    // mock someOtherFoo() to ensure it gets the correct value for $arg
    $created = $this->getMock('someClass', array('someOtherFoo'));
    $created->expects($this->once())
            ->method('someOtherFoo')
            ->with('foo');
    // mock createSomeClass() to return the mock above
    $creator = $this->getMock('someClass', array('createSomeClass'));
    $creator->expects($this->once())
            ->method('createSomeClass')
            ->will($this->returnValue($created));
    // call someFoo() with the correct $arg
    $creator->someFoo('foo');
}

请记住,因为实例正在创建同一个类的另一个实例,所以通常会涉及两个实例。如果更清楚的话,您可以在这里使用相同的模拟实例。

function testSomeFoo() {
    $fixture = $this->getMock('someClass', array('createSomeClass', 'someOtherFoo'));
    // mock createSomeClass() to return the mock
    $fixture->expects($this->once())
            ->method('createSomeClass')
            ->will($this->returnValue($fixture));
    // mock someOtherFoo() to ensure it gets the correct value for $arg
    $fixture->expects($this->once())
            ->method('someOtherFoo')
            ->with('foo');
    // call someFoo() with the correct $arg
    $fixture->someFoo('foo');
}

可以用overload:作为模拟类名的前缀

查看Mocking Hard Dependencies的文档。

你的例子应该是这样的:

/**
 * @runTestsInSeparateProcesses
 * @preserveGlobalState disabled
 */
class SomeClassTest extends 'PHPUnit'Framework'TestCase
{
    public function test_some_foo()
    {
        $someOtherClassMock = 'Mockery::mock('overload:SomeOtherClass');
        $someOtherClassMock->shouldReceive('someOtherFoo')
            ->once()
            ->with('something')
            ->andReturn();
        $systemUnderTest = new SomeClass();
        $systemUnderTest->someFoo('something');
    }
}

我添加了@runTestsInSeparateProcesses注释,因为通常模拟类也将在其他测试中使用。如果没有注释,自动加载程序就会因为class already exists错误而崩溃。

如果这是您的测试套件中唯一使用mock类的地方,那么您应该删除注释。

我在这里找到了我的方法,试图白盒测试一个类__构造函数,以确保它调用自己的类方法,并将一些数据传递给__构造函数。

如果其他人出于同样的原因在这里,我想我会分享我最终使用的方法(没有在这个问题中使用的工厂式createSomeClass()方法)。

<?php
class someClass {
  public function __constructor($param1) {
    // here is the method in the constructor we want to call
    $this->someOtherFoo($param1);
  }
  public function someOtherFoo($var){  }
}
现在PHPUnit测试:
<?php
$paramData = 'someData';
// set up the mock class here
$model = $this->getMock('someClass', 
  array('someOtherFoo'), // override the method we want to check
  array($paramData) // we need to pass in a parameter to the __constructor
);
// test that someOtherFoo() is called once, with out test data
$model->expects($this->once())
      ->with($paramData)
      ->method('someOtherFoo');
// directly call the constructor, instead of doing "new someClass" like normal
$model->__construct($paramData);