【发布时间】:2016-07-22 01:43:57
【问题描述】:
我正在尝试使用 PHPUnit 测试以下类
class stripe extends paymentValidator {
public $apiKey;
public function __construct ($apiKey){
$this->apiKey = $apiKey;
}
public function charge($token) {
try {
return $this->requestStripe($token);
} catch(\Stripe\Error\Card $e) {
echo $e->getMessage();
return false;
}
}
public function requestStripe($token) {
// do something
}
}
我的测试脚本如下:
class paymentvalidatorTest extends PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function test_stripe() {
// Create a stub for the SomeClass class.
$stripe = $this->getMockBuilder(stripe::class)
->disableOriginalConstructor()
->setMethods(['requestStripe', 'charge'])
->getMock();
$stripe->expects($this->any())
->method('requestStripe')
->will($this->returnValue('Miaw'));
$sound = $stripe->charge('token');
$this->assertEquals('Miaw', $sound);
}
}
使用我的测试脚本,我期望 stripe::charge() 方法的测试替身将完全按照原始类中的定义执行,并且 stripe::requestStripe() 将返回“Miaw”。因此,$stripe->charge('token') 也应该返回 'Miaw'。但是,当我运行测试时,我得到:
Failed asserting that null matches expected 'Miaw'.
我应该如何解决这个问题?
【问题讨论】:
-
你没有设置
charge方法返回任何东西,因此它返回null。
标签: php unit-testing phpunit