gpt4 book ai didi

Laravel 4 phpunit 测试 : Test flagged as `risky` for not closing its own output buffers

转载 作者:行者123 更新时间:2023-12-04 05:30:38 24 4
gpt4 key购买 nike

我在尝试在 phpunit 测试中测试简单的 Illuminate\Http\Response 对象时遇到问题。有问题的错误是:

PHPUnit 4.3-dev by Sebastian Bergmann.

Configuration read from /var/MyApp/phpunit.xml

....................R.................

Time: 2.71 seconds, Memory: 76.75Mb

OK, but incomplete, skipped, or risky tests!
Tests: 38, Assertions: 55, Risky: 1.

运行时显示 R 的地方 phpunit --tap作为
# RISKY Test code or tested code did not (only) close its own output buffers

我要测试的类(class)是:
class PrettyError {

/**
* Renders a pretty 500 error page view
*
* @return string with error view
*/
public function render()
{
return Response::make(View::make('viewName')->with('param','value')->render(), 500, array());
}
}

测试是
class Pretty500Test extends BaseTest {

/** @var Pretty500 */
private $pretty500;

/**
* Set dependencies
*/
public function __construct()
{
$this->pretty500 = app(PrettyError::class);
}

public function testRender()
{
$response = $this->pretty500->render();

//assertions below, never get reached cause tests aborts after above call
$this->assertsTrue('500',$response->getStatus());
}

}

请注意,使用 laravel 在我的应用程序中测试任何路由时会发生相同的错误
this->route('GET','routeName')
helper 方法,所以这似乎是与 Response 请求相关的一般错误。据我所知,我的应用程序中没有任何东西对输出缓冲(有目的地)做任何奇怪的事情,这是我能想到的唯一可能会破坏 Response 类的标准工作的东西。

我正在使用 phpunit 4.3-dev、laravel 4.2.1 和 mockery 进行测试。

我在互联网上根本没有找到关于这个特定风险错误代码的内容,所以我在没有进一步帮助的情况下不知所措。

最佳答案

不够好的封闭三元运算符 (?)

在 Laravel 5 中进行测试时,我遇到了与风险测试用例相同的问题。 这里的问题是,我的导航中有如下代码(默认布局文件):

<li{{ Request::is('clients*')?' class="active"':'' }}><a href="/clients">Clients</a></li>

测试不喜欢这个部分,因为三元运算符( ?)没有很好地关闭。我将其替换为以下内容以使其正常工作(在三元运算符部分周围添加了括号):
<li{{ (Request::is('clients*')?' class="active"':'') }}><a href="/clients">Clients</a></li>

我通过从请求的 View 中逐个删除 Blade 语法、它扩展的布局和包含的文件来发现这一点。在删除上面的语法时,它起作用了,所以出现了错误。

空产量/节

发生了同样的错误,当我为产量传递一个空值时,例如在以下场景中:

layout.blade.php
<h1>@yield('title')</h1>

page.blade.php
@extends('layout')    
@section('title','') //<-- This empty value raised the error

其他需要考虑的事项

也许很清楚,但以防万一你犯了同样的错误:你必须检查所有涉及的文件。

我运行了以下测试,它访问登录页面,输入电子邮件和密码并按下登录按钮:

测试/AuthenticationTest.php
<?php
class AuthenticationTest extends TestCase
{
// [...]

public function testLoginSuccessful()
{
$password = str_random();

$user = factory(App\User::class)->create(['password' => $password]);

$this->visit(route('login'))
->type($user->email, 'email')
->type($password, 'password')
->press('Login')
->seePageIs(route('dashboard2'));
}
}

app/Http/routes.php
<?php
Route::get('auth/login', 'Auth\AuthController@getLogin')->name('login');
Route::post('auth/login', 'Auth\AuthController@postLogin');

app/Http/Controllers/Auth/AuthController.php
<?php    
// [...]
class AuthController extends Controller
{
// [...]

protected $redirectPath = '/dashboard2'; // <-- check this file as well
}

所以我必须特别检查以下我进行修改的文件:
  • app/Http/routes.php
  • app/Http/Controllers/Auth/AuthController.php -> 函数 getLogin
  • resources/views/auth/login.blade.php(在 Auth\AuthContoller@getLogin 中返回)
  • app/Http/Controllers/Auth/AuthController.php -> 函数 postLogin
  • resources/views/dashboard2.blade.php(登录成功返回)
  • 关于Laravel 4 phpunit 测试 : Test flagged as `risky` for not closing its own output buffers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24405719/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com