【问题标题】:Laravel test without exception, throws exceptionLaravel 测试无异常,抛出异常
【发布时间】:2020-08-18 03:32:41
【问题描述】:

我想使用 Laravel 和 PHPUnit 测试数据库中缺失的记录。

我已设置我的测试并设置该方法引发异常。测试报错,因为异常已经抛出。

这是我的测试:

/** @test */
    public function myTest()
    {
        $this->withoutExceptionHandling();
        $this->_createMemberRequest(['email_address' => $this->email_address]);
    }

测试结果:

有 1 个错误:

1) 我的测试

Exceptions\InvalidEmailException:电子邮件已存在

我做错了什么?

感谢您的帮助!

【问题讨论】:

  • 使用 withoutExceptionHandling 获取有关此问题发生原因的详细信息的要点。所以你的测试已经告诉你问题出在哪里。您首先需要解决该问题,然后继续进行测试,直到找到您正在寻找的适当异常。

标签: laravel testing exception phpunit


【解决方案1】:

$this->withoutExceptionHandling(); 的全部意义在于当你遇到一个时髦的错误并且你不知道该怎么做或者错误不够清楚但如果你正在尝试测试你的表单验证 ('name' => 'required') 或类似的东西存储请求时,您必须删除 $this->withoutExceptionHandling(); 才能通过测试。

【讨论】:

    猜你喜欢
    • 2014-03-25
    • 2014-05-10
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 2013-09-08
    • 2011-01-05
    • 2018-03-31
    相关资源
    最近更新 更多