Php 为需要在函数中进行实体模型的现有函数编写单元测试类
我有很多PHP函数,需要编写单元测试类。 我只是想知道,在使用断言之前,有没有办法对函数行进行模拟Php 为需要在函数中进行实体模型的现有函数编写单元测试类,php,unit-testing,testing,phpunit,Php,Unit Testing,Testing,Phpunit,我有很多PHP函数,需要编写单元测试类。 我只是想知道,在使用断言之前,有没有办法对函数行进行模拟 <?php function get_Func($args){ $number = $args[0]; $id = "71".$args[1]."0"; //The Line I need to mockup without touching the code $result = getViaCurl($number,$i
<?php
function get_Func($args){
$number = $args[0];
$id = "71".$args[1]."0";
//The Line I need to mockup without touching the code
$result = getViaCurl($number,$id);
return $result;
}
?>
您需要将函数提取到服务中,这样您就可以轻松地模拟它,但只要您的函数在类中,因为这样您就可以将新服务传递给类构造函数,以便能够在测试中通过模拟,而不是原来的模拟
class YourClass
{
private NewService $service;
public function __construct($service)
{
$this->service = $service;
}
function get_Func($args)
{
$number = $args[0];
$id = "71".$args[1]."0";
//The Line I need to mockup without touching the code
$result = $this->service->getViaCurl();
return $result;
}
}
class NewService
{
public function getViaCurl($number, $id)
{
return getViaCurl($number,$id);
}
}
我猜的是
$id=“71”+$args[1]+“0”代码>正在尝试连接,而不是进行添加!因此PHP连接符是一个
(点)而不是+
(加号)