在 laravel5.1 上重定向时 Phpunit 失败

Phpunit fails when redirect on laravel5.1

当我 运行 phpunit 时,即使我在实际的浏览器上进行了测试,它还是失败了。

我期望的是当用户注册治疗师,输入所需信息并按下按钮然后 t运行 坐到下一个屏幕将 him/her 分配给 b运行 ch.

谁能解释一下哪里出了问题?

phpunit的栈跟踪如下:

PHPUnit 5.0.10 by Sebastian Bergmann and contributors.

F.                                                                  2 / 2 (100%)

Time: 2.46 seconds, Memory: 24.25Mb

There was 1 failure:

1) TherapistsControllerTest::testCreate
A request to [http://nuatthai.loc/therapists/77/assign] failed. Received status code [500].

/Users/kondonator/Documents/htdocs/nuatthai/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:165
/Users/kondonator/Documents/htdocs/nuatthai/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:63
/Users/kondonator/Documents/htdocs/nuatthai/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:109
/Users/kondonator/Documents/htdocs/nuatthai/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:63
/Users/kondonator/Documents/htdocs/nuatthai/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:85
/Users/kondonator/Documents/htdocs/nuatthai/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:658
/Users/kondonator/Documents/htdocs/nuatthai/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:645
/Users/kondonator/Documents/htdocs/nuatthai/tests/TherapistsControllerTest.php:21

Caused by
exception 'ErrorException' with message 'Undefined index: REQUEST_URI' in /Users/kondonator/Documents/htdocs/nuatthai/storage/framework/views/9eeab8e0b5ecca2e976774be0813d5d7:24
Stack trace:
#0 /Users/kondonator/Documents/htdocs/nuatthai/storage/framework/views/9eeab8e0b5ecca2e976774be0813d5d7(24): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined index...', '/Users/kondonat...', 24, Array)
#1 /Users/kondonator/Documents/htdocs/nuatthai/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php(42): include('/Users/kondonat...')
#2 /Users/kondonator/Documents/htdocs/nuatthai/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(58): Illuminate\View\Engines\PhpEngine->evaluatePath('/Users/kondonat...', Array)
#3 /Users/kondonator/Documents/htdocs/nuatthai/vendor/laravel/framework/src/Illuminate/View/View.php(135): Illuminate\View\Engines\CompilerEngine->get('/Users/kondonat...', Array)
#4 /Users/kondonator/Documents/htdocs/nuatthai/vendor/laravel/framework/src/Illuminate/View/View.php(106): Illuminate\View\View->getContents()
:

测试是这样的:

12    public function testCreate()
13    {
14      $user = self::getOwner();
15      $this->assertNotNull($user);
16      $this->actingAs($user)
17              ->visit('/therapists/register')
18              ->see('Register New Therapist')
19              ->type('Test Therapist 4', 'name')
20              ->select('false', 'lmt')
21              ->press('Register')
22              ->seePage('New therapist was registered. Please assign the branches that he / she belongs to.')
23              ->seePage('Assign Branches to Therapist')
24              ;
25    }

而blade代码是这样的:

@section('content')
          @include('common.message')

          <h1>Register New Therapist</h1>

          {!! Form::open(array('action' => ['TherapistsController@doRegister'])) !!}
          @include('therapists.form', ['modifier' => 'required'])

            <div style="text-align:right;">
              {!! Form::submit('Register', ['class' => 'submit btn btn-primary btn-lg']) !!}
            </div>
          {!! Form::close() !!}
@endsection

那么controller的代码是这样的:

  public function doRegister(Request $request)
  {
    $therapist = self::createData($request);
    \Session::flash('flash_success', 'New therapist was registered. Please assign the branches that he / she belongs to.');
    return redirect()->route('therapists.preAssign', $therapist->therapist_id);
  }

我也有类似的代码,但没有任何错误。

测试代码如下:

  public function testCreate()
  {
    $user = self::getAccountant();
    $this->assertNotNull($user);
    $this->actingAs($user)
            ->visit('/categories/create')
            ->see('Add New Expense Category')
            ->type(self::CATEGORY_NAME, 'name')
            ->press('Add')
            ->see('View Existing Expense Category')
            ->see('New category was registered.')
            ->see(self::CATEGORY_NAME)
            ;

  }

blade如下:

@section('content')
          @include('common.message')

          <h1>Add New Expense Category</h1>

          {!! Form::open(array('action' => ['CategoriesController@doCreate'])) !!}
            @include('categories.form', ['modifier' => 'required'])

            <div style="text-align:right;">
              {!! Form::submit('Add', ['class' => 'submit btn btn-primary btn-lg']) !!}
            </div>
          {!! Form::close() !!}

@endsection

控制器如下:

  public function doCreate(Request $request)
  {
    $name = $request->name;
    $category = Category::create(['name' => $name]);
    $item = Item::create(['name' => $name]);
    CategoryItem::create(["category_id" => $category->category_id, "item_id" => $item->item_id]);
    \Session::flash('flash_success', 'New category was registered. As default, the same item is also registered under the category.');
    return redirect()->route('categories.show', [$category]);    
  }

感谢您的支持。

phpunit 告诉你,当它试图访问 url http://nuatthai.loc/therapists/77/assign 时,它 运行 在视图文件中出现错误:Undefined index: REQUEST_URI

因此,在我看来,无论您在那个 url 加载什么视图,都试图访问 $_SERVER['REQUEST_URI'],而没有先检查 REQUEST_URI 键是否存在。

$_SERVER 数组中的条目永远不会保证 运行 存在。在访问之前检查是否存在总是一个好主意。