简体   繁体   English

使用laravel运行单元测试时,如何测试App :: error()实现?

[英]When running unit tests with laravel, how do you test your App::error() implementations?

I'm currently working on an open source personal project that provides a nice backend api for game developers. 我目前正在从事一个开放源代码的个人项目,该项目为游戏开发人员提供了一个不错的后端api。 I'm in the early stages of development, but I plan to write tests as I go along, which is where I've hit a snag. 我处于开发的早期阶段,但是我计划在进行过程中编写测试,这是我遇到的障碍。

Through out the system when an error occurs such as incorrect api credentials or missing credentials, I throw a custom exception which stores a bit of extra data so that I can catch it and give a JSON encoded response. 当发生错误(例如错误的api凭据或缺少凭据)时,系统会抛出一个自定义异常,该异常存储了一些额外的数据,因此我可以捕获它并给出JSON编码的响应。

The tests work fine for those thrown in my BaseController, but I also capture a few Laravel Exceptions so I can respond with my own, or at least, output JSON like below: 这些测试对于在我的BaseController中抛出的测试工作正常,但是我也捕获了一些Laravel异常,因此我可以使用自己的(或至少是)输出JSON进行响应,如下所示:

app/start/global.php 应用程序/启动/ global.php

App::error(function(Exception $exception, $code) {
    Log::error($exception);
});

App::missing(function(Exception $exception) {
    return BaseController::error(
            Config::get('response.method.code'), 
            Config::get('response.method.http'), 
            'Method not found'
    );
});

App::error(function(Viper\Exception $exception) {
    return BaseController::error(
            $exception->getCode(), 
            $exception->getStatusCode(), 
            $exception->getMessage()
    );
});

I'm using the try { } catch() { } approach as I need to check an extra value that isn't in the normal Exceptions. 我正在使用try { } catch() { }方法,因为我需要检查正常Exception中没有的额外值。

public function testNoMethodGET() {
    $config = Config::get('response.method');

    try {
        $this->call('GET', '/');
    } catch(\Viper\Exception $e) {
        $this->assertEquals($e->getCode(), $config['code']);
        $this->assertEquals($e->getStatusCode(), $config['http']);
    }

    $this->fail('Exception not thrown');
}

This is all good and well, but I want to check a few things on the actual response, like for example, whether or not the json is valid, whether or not the response structure matches and whether or not the response values are correct. 一切都很好,但是我想检查一下实际响应中的一些内容,例如json是否有效,响应结构是否匹配以及响应值是否正确。

If I set the return value of $this->call() to a variable, I'd be unable to access that variable within the catch block, so the question is this, how can I test the return value of $this->call() once the Exception has been caught? 如果我将$this->call()的返回值设置为变量,则无法在catch块中访问该变量,所以问题是这样的, 如何测试$this->call()的返回值捕获到异常后立即$this->call()

According to Taylor Otwell: 根据泰勒·奥特威尔(Taylor Otwell)的说法:

"this can be solved by de-coupling your test. You really want to test the handler and that the exception is thrown totally separately anyways [sic] to isolate your tests. For instance: “这可以通过取消测试的耦合来解决。您确实想测试处理程序,并且无论如何,都会将异常完全单独抛出以隔离测试。例如:

App::error(function(ErrorType $e)
{
     App::make('ErrorTypeHandler')->handle($e);
});

Now you can write test cases for ErrorTypeHandler class separately from the rest of your application. 现在,您可以与应用程序的其余部分分开编写ErrorTypeHandler类的测试用例。 Then check that proper exceptions are thrown by your app with @expectedException." 然后使用@expectedException检查应用是否抛出了适当的异常。”

In your case, you already have isolated your error handler in BaseController::error(), so you can test the responses directly in separate unit tests, without the use of $this->call(). 在您的情况下,您已经在BaseController :: error()中隔离了错误处理程序,因此您可以直接在单独的单元测试中测试响应,而无需使用$ this-> call()。 Instead, just call $response = BaseController::error() with the desired parameters and then inspect the response and apply relevant assertions. 相反,只需使用所需的参数调用$ response = BaseController :: error(),然后检查响应并应用相关的断言即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel单元测试:运行多个测试方法时重新定义错误 - Laravel unit tests: redefinition error when running multiple test methods 您在更新原始源代码后是否更新了单元测试? - Do you update your Unit Tests after updating your original source code? Laravel单元测试未在Windows上运行 - Laravel unit tests not running on windows 如何测试验证错误在 laravel 单元测试中抛出确切的错误和消息 - How to test validation errors throw exact error and message in laravel unit tests 在 Laravel 8 中运行多个 PHPUnit 测试时出错 - Error when running multiple PHPUnit Tests in Laravel 8 您是否在验收测试或单元测试中测试所有无效电子邮件和密码的组合,以进行登录? - Do you test all combinations of invalid emails and passwords in Acceptance tests or in Unit tests for a login? 你如何使用 laravel-jsvalidation 显示你自己的错误信息? - How do you display your own error messages with laravel-jsvalidation? 你如何编写和测试正则表达式? - How do you write and test your regular expressions? 在 Laravel 8 的单元测试中调用未定义的方法 Tests\\Unit\\TopPageTest::get() - Call to undefined method Tests\Unit\TopPageTest::get() in Unit test in Laravel 8 在ubuntu中运行laravel应用时出错 - Get error when running a laravel app in ubuntu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM