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

时间:2016-01-29 04:25:24

标签: php laravel phpunit

当我运行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]);    
  }

我提前感谢您的支持。

1 个答案:

答案 0 :(得分:0)

phpunit告诉您,当它尝试访问网址http://nuatthai.loc/therapists/77/assign时,它会在视图文件中遇到错误:Undefined index: REQUEST_URI

因此,在我看来,无论您在该网址上加载什么视图,都会尝试访问$_SERVER['REQUEST_URI']而不先检查REQUEST_URI密钥是否存在。

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