使用Laravel PHPUnit press()函数时出现404错误

时间:2016-01-25 21:32:26

标签: php laravel phpunit

我正在尝试在开发环境中运行phpunit。但是,每当我有一个使用press()函数的函数时,我都会收到404错误。

1) LoginTest::testLoginWithInvalidCredentials  
A request to [http://localhost/set/public/auth/login] failed. Received status code [404].

C:\inetpub\wwwroot\set\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:165 
C:\inetpub\wwwroot\set\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:63 
C:\inetpub\wwwroot\set\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:85 
C:\inetpub\wwwroot\set\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:658 
C:\inetpub\wwwroot\set\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:645 
C:\inetpub\wwwroot\set\tests\LoginTest.php:49

Caused by exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' in C:\inetpub\wwwroot\set\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php:161

当我访问http://localhost/set/public/auth/login时,网址正常。事实上,当我利用网站时,一切正常。另外,如果我在本地计算机上运行phpunit,我根本不会收到错误。

我检查了我的.env文件,所有内容都检查系统是否正常工作。我的路线看起来也很正确。如果我手动运行测试,一切都正常。它正在运行这个失败的phpunit测试。

非常感谢任何关于去哪儿的方向。

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class LoginTest extends TestCase
{
    use DatabaseTransactions;

    public function testLoginPageOk()
    {
        $this->visit('/')           
           ->see('You are accessing');
    }

    public function testRedirectToLoginPage()
    {
        $this->call('GET','/');
        $this->assertRedirectedTo('auth/login');

   }

    public function testAuthLoginRoutes()
    {
        $this->call('GET', '/auth/login');
        $this->see('You are accessing');
    }

    public function testLoginWithInvalidCredentials()
    {
        $this->visit('/auth/login')
            ->type('asdf', 'username')
            ->type('gibberish', 'password')
            ->press('Sign in')
            ->see('These credentials do not match our records.');
    }
}

这是我的路线档案:

<?php 

// This group requires a user to be logged in.
Route::group(['middleware' => 'auth'], function() {

    // when you login, if you were not going to a page, you get directed 
    // to /home. Let's push that to the actual home page.
    Route::get('home', function(){ return redirect('/');});

    Route::get('/', 'HomeController@index');
});

// Authentication routes. Doesn't need to be logged in.
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

最后我的.env文件如下:

APP_ENV=local
APP_DEBUG=true
APP_KEY=ycDUbCfgo81OJfc1TjNBjggAtXqwES5I
APP_URL=http://localhost/set/public/

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=ilsausmail
MAIL_PORT=25
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

1 个答案:

答案 0 :(得分:0)

更新:原来这是我登录表单中的代码。

我把它当作:

action="/auth/login"

但是,我没有在根域运行laravel,而是在几个目录中运行。一旦我将其修复为以下它就可以了。

action="{{ url('/auth/login') }}"