问题描述
我为 PHP Trait 类编写测试。当我在 PHPStorm IDE 上运行覆盖率测试时,它在编辑器行号上显示一些红线,并在这样的覆盖率测试行上给我 76% 的结果
我的问题是如何在这些行上进行测试,这样我才能得到 100% 行的结果
这是我的测试代码:
响应.PHP
<?PHP
trait Response {
/**
* Failed to login response.
*
* @return array
*/
public function FailedLogin(): array
{
return [
'code' => 401,'message' => 'Failed to login',];
}
}
ResponseTest.PHP
<?PHP
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class ResponseTest extends TestCase
{
/**
* @var MockObject|Response|null
*/
private ?MockObject $mock;
protected function setUp(): void
{
$this->mock = $this->getMockForTrait(Response::class);
}
/**
* @test
*/
public function it_correct_output_Failed_login()
{
$response = $this->mock->FailedLogin();
$this->assertResponse($response,401,'Failed to login');
}
/**
* Assert response.
*
* @param array $response
* @param int $code
* @param null $message
*/
private function assertResponse(array $response,$code = 200,$message = null)
{
$this->assertIsArray($response);
$this->assertArrayHasKey('code',$response);
$this->assertArrayHasKey('message',$response);
$this->assertContains($code,$response);
$this->assertContains($message,$response);
}
protected function tearDown(): void
{
$this->mock = null;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)