Laravel 4 phpunit test:测试标记为"risky",因为它没有关闭自己的输出缓冲区

Ina*_*aki 8 phpunit laravel laravel-4

我在尝试在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.     
Run Code Online (Sandbox Code Playgroud)

其中运行时,将R被示出phpunit --tap

# RISKY Test code or tested code did not (only) close its own output buffers
Run Code Online (Sandbox Code Playgroud)

我要测试的课程是:

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

而测试是

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());
    }

}    
Run Code Online (Sandbox Code Playgroud)

请注意,使用laravel测试我的应用程序中的任何路径时会发生相同的错误

this->route('GET','routeName')

帮助器方法,所以这似乎是与Response请求相关的一般错误.据我所知,我的应用程序中没有任何东西可以通过输出缓冲(故意)做任何有趣的事情,这是我唯一能想到的可能会破坏Response类的标准工作.

我正在使用phpunit 4.3-dev,laravel 4.2.1和测试的嘲弄.

就这个特殊的风险错误代码而言,我在互联网上找不到太多东西,所以在没有进一步帮助的情况下我感到很茫然.

Ale*_*orb 8

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

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

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

测试不喜欢这个部分,因为三元运算符(the ?)没有很好地关闭.我用以下内容替换它以使其工作(在三元运算符部分周围添加了parantheses):

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

我通过从请求的视图中部分删除刀片语法,扩展它的布局以及包含的文件来找到这个.在删除上面的语法时,它工作,所以有错误.

空产量/部分

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

layout.blade.php

<h1>@yield('title')</h1>
Run Code Online (Sandbox Code Playgroud)

page.blade.php

@extends('layout')    
@section('title','') //<-- This empty value raised the error
Run Code Online (Sandbox Code Playgroud)

其他需要考虑的事情

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

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

测试/ 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'));
    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序/ HTTP/routes.php文件

<?php
Route::get('auth/login',    'Auth\AuthController@getLogin')->name('login');
Route::post('auth/login',   'Auth\AuthController@postLogin');
Run Code Online (Sandbox Code Playgroud)

应用程序/ HTTP /控制器/认证/ AuthController.php

<?php    
// [...]
class AuthController extends Controller
{
    // [...]

    protected $redirectPath = '/dashboard2'; // <-- check this file as well
}
Run Code Online (Sandbox Code Playgroud)

所以我必须特别检查以下修改过的文件:

  • 应用程序/ HTTP/routes.php文件
  • app/Http/Controllers/Auth/AuthController.php - > function getLogin
  • resources/views/auth/login.blade.php(在Auth\AuthContoller @ getLogin中返回)
  • app/Http/Controllers/Auth/AuthController.php - >函数postLogin
  • resources/views/dashboard2.blade.php(成功登录时返回)

  • 啊!@ section('title','')`引起了问题.谢谢! (2认同)

you*_*es0 6

在Laravel 5中
也会发生.当响应格式不正确时,PHPUnit将抛出此内容.

这可能是由于:

  • Laravel app中的错误:检查日志
  • 刀片语法不正确:例如在我的情况下,有一个过多的刀片@stop,Laravel不会将其视为错误(并且未记录)


Ina*_*aki 1

事实证明,问题是由于使用了有问题的视图造成的

Input::old()

在表格内。在尝试简单地访问Input::old('foo')任何 phpunit 测试后,我得到了

RuntimeException: Session store not set on request.

因此,真正的罪魁祸首是我的单元测试中的会话存储,而不是任何请求/视图渲染问题。通过在 testRender() 方法中设置当前 Request 实例的会话存储解决了问题:

\Illuminate\Support\Facades\Request::setSession($this->app['session.store'])