模拟正在测试的 class 的 public 方法
Mock a public method of the class being tested
我正在尝试对 class 进行单元测试,其中一个 public 方法(方法 1)使用相同 class 的另一个 public 方法(方法 2)在里面。例如
class MyClass {
public function method1()
{
$var = $this->method2();
// Do stuff with var to get $answer
return $answer;
}
public function method2()
{
// Do complex workings out to get $var
return $var;
}
}
现在,method2
已经进行了单元测试,我不想为它构建一个参数以在测试 method1
时实际调用它。我只想模拟 method2
并在我的测试中定义将返回的内容。这是我的:
function test_method1()
{
$myClass = new MyClass();
$mockedMyClass = Mockery::mock('MyClass');
$mockedMyClass->shouldReceive('method2')->once()->andReturn([1,2,3,4]); // mocked $var returned to be used in the rest of method1
$answer = $myClass->method1();
// Assertions
}
显然这不起作用,因为被测试的方法与包含被模拟的方法在同一个 class 中,因此无法将模拟的 class 作为依赖项传递。仅模拟 method2 的最佳方法是什么?
您可以使用测试框架的 Partial Mock 功能来测试您被标记为模拟的 class。
例如,假设您修改了 class:
<?php
namespace Acme\DemoBundle\Model;
class MyClass {
public function method1()
{
$var = $this->method2();
$answer = in_array(3, $var);
// Do stuff with var to get $answer
return $answer;
}
public function method2()
{
// Do complex workings out to get $var
return array();
}
}
并测试如下:
<?php
namespace Acme\DemoBundle\Tests;
class MyClassTest extends \PHPUnit_Framework_TestCase {
function test_method1()
{
$mockedMyClass = \Mockery::mock('Acme\DemoBundle\Model\MyClass[method2]');
$mockedMyClass->shouldReceive('method2')->once()->andReturn([1,2,3,4]); // mocked $var returned to be used in the rest of method1
$answer = $mockedMyClass->method1();
$this->assertTrue($answer); // Assertions
}
}
希望对您有所帮助
我正在尝试对 class 进行单元测试,其中一个 public 方法(方法 1)使用相同 class 的另一个 public 方法(方法 2)在里面。例如
class MyClass {
public function method1()
{
$var = $this->method2();
// Do stuff with var to get $answer
return $answer;
}
public function method2()
{
// Do complex workings out to get $var
return $var;
}
}
现在,method2
已经进行了单元测试,我不想为它构建一个参数以在测试 method1
时实际调用它。我只想模拟 method2
并在我的测试中定义将返回的内容。这是我的:
function test_method1()
{
$myClass = new MyClass();
$mockedMyClass = Mockery::mock('MyClass');
$mockedMyClass->shouldReceive('method2')->once()->andReturn([1,2,3,4]); // mocked $var returned to be used in the rest of method1
$answer = $myClass->method1();
// Assertions
}
显然这不起作用,因为被测试的方法与包含被模拟的方法在同一个 class 中,因此无法将模拟的 class 作为依赖项传递。仅模拟 method2 的最佳方法是什么?
您可以使用测试框架的 Partial Mock 功能来测试您被标记为模拟的 class。 例如,假设您修改了 class:
<?php
namespace Acme\DemoBundle\Model;
class MyClass {
public function method1()
{
$var = $this->method2();
$answer = in_array(3, $var);
// Do stuff with var to get $answer
return $answer;
}
public function method2()
{
// Do complex workings out to get $var
return array();
}
}
并测试如下:
<?php
namespace Acme\DemoBundle\Tests;
class MyClassTest extends \PHPUnit_Framework_TestCase {
function test_method1()
{
$mockedMyClass = \Mockery::mock('Acme\DemoBundle\Model\MyClass[method2]');
$mockedMyClass->shouldReceive('method2')->once()->andReturn([1,2,3,4]); // mocked $var returned to be used in the rest of method1
$answer = $mockedMyClass->method1();
$this->assertTrue($answer); // Assertions
}
}
希望对您有所帮助