求解?《LX2 PHP 扩展包实战教程 - 从入门到发布》- 12 编写单元测试

    // 检查 $type 参数
    public function testGetWeatherWithInvalidType()
    {
        $w = new Weather('mock-key');

        // 断言会抛出此异常类
        $this->expectException(InvalidArgumentException::class);

        // 断言异常消息为 'Invalid type value(base/all): foo'
        $this->expectExceptionMessage('Invalid type value(base/all): foo');

        $w->getWeather('深圳', 'foo1');

        $this->fail('Failed to assert getWeather throw exception with invalid argument.');
    }

这个单元测试为什么会通过呢?注意foo1和foo

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

应该是 expectExceptionMessage 检查逻辑是包含而不是全等。

4年前 评论
讨论数量: 2

应该是 expectExceptionMessage 检查逻辑是包含而不是全等。

4年前 评论

确实是这样,内部使用 strpos 方法进行判断的。

protected function matches($other): bool
{
        if ($this->expectedMessage === '') {
            return $other->getMessage() === '';
        }
        return \strpos((string) $other->getMessage(), $this->expectedMessage) !== false;
}
4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!