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


Phpunit fails when redirect on laravel5.1

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

我期望的是当用户注册治疗师时,输入所需的信息并按下按钮,然后转到下一个屏幕以将他/她分配到分支。

谁能解释一下出了什么问题?

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    }

刀片代码是这样的:

@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

那么控制器的代码是这样的:

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

刀片如下:

@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数组中的条目从不保证存在。在访问之前检查是否存在始终是一个好主意。