我在运行PHPUnit测试时出现问题,但在尝试手动测试应用程序时却没有。
我的单元测试包含
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class UserAdminPanelTest extends TestCase
{
use DatabaseMigrations;
/**
* Tests that the user/company admin panel is showing data and able to save changes.
*
* @return void
*/
public function testUserAdminPanel()
{
$user = factory(App\User::class)->create();
$this->actingAs($user)
->withSession(['foo' => 'bar'])
->visit('/')
->click('User')
->seePageIs('/user/3')
->see('Edit')
->click('Edit'); // There's more after this but this is where I get the error
}
}
我的路线是
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', function () {
return view('welcome');
});
Route::get('/home', 'HomeController@index');
Route::group(['middleware' => 'auth'], function () {
Route::resource('user', 'UserController',
['except' => ['create', 'store', 'destroy']]);
});
});
我尝试运行测试时得到的错误是
There was 1 failure:
1) UserAdminPanelTest::testUserAdminPanel
A request to [http://localhost/usbsinc/public/user/3/edit] failed. Received status code [404].
C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php:178
C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php:72
C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php:53
C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php:706
C:\xampp\htdocs\usbsinc\tests\UserAdminPanelTest.php:27
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:129
Caused by
exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' in C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php:161
Stack trace:
#0 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Routing\Router.php(823): Illuminate\Routing\RouteCollection->match(Object(Illuminate\Http\Request))
#1 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Routing\Router.php(691): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))
#2 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Routing\Router.php(675): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#3 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(246): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#4 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#5 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#6 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(44): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#7 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#8 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#9 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#10 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#11 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#12 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#13 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(132): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#14 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(99): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#15 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php(480): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#16 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php(70): Illuminate\Foundation\Testing\TestCase->call('GET', 'http://localhos...', Array, Array, Array)
#17 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php(53): Illuminate\Foundation\Testing\TestCase->makeRequest('GET', 'http://localhos...')
#18 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php(706): Illuminate\Foundation\Testing\TestCase->visit('http://localhos...')
#19 C:\xampp\htdocs\usbsinc\tests\UserAdminPanelTest.php(27): Illuminate\Foundation\Testing\TestCase->click('Edit')
#20 [internal function]: UserAdminPanelTest->testUserAdminPanel()
#21 C:\xampp\htdocs\usbsinc\vendor\phpunit\phpunit\src\Framework\TestCase.php(909): ReflectionMethod->invokeArgs(Object(UserAdminPanelTest), Array)
#22 C:\xampp\htdocs\usbsinc\vendor\phpunit\phpunit\src\Framework\TestCase.php(768): PHPUnit_Framework_TestCase->runTest()
#23 C:\xampp\htdocs\usbsinc\vendor\phpunit\phpunit\src\Framework\TestResult.php(612): PHPUnit_Framework_TestCase->runBare()
#24 C:\xampp\htdocs\usbsinc\vendor\phpunit\phpunit\src\Framework\TestCase.php(724): PHPUnit_Framework_TestResult->run(Object(UserAdminPanelTest))
#25 C:\xampp\htdocs\usbsinc\vendor\phpunit\phpunit\src\Framework\TestSuite.php(747): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#26 C:\xampp\htdocs\usbsinc\vendor\phpunit\phpunit\src\Framework\TestSuite.php(747): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#27 C:\xampp\htdocs\usbsinc\vendor\phpunit\phpunit\src\TextUI\TestRunner.php(440): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#28 C:\xampp\php\pear\PHPUnit\TextUI\Command.php(176): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#29 C:\xampp\php\pear\PHPUnit\TextUI\Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
#30 C:\xampp\php\phpunit(46): PHPUnit_TextUI_Command::main()
#31 {main}
我已挖掘并挖掘但我无法弄清楚发生了什么,甚至 如何这样的事情可能会发生。有关某些调试代码或可能发生的事情的任何提示?
答案 0 :(得分:3)
我遇到了同样的问题并以这种方式解决了问题:
转到 /vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php第140行
通过添加此代码并从命令行运行phpunit来转储并消除函数匹配(Request $ request)中的$ request。
dd($request);
现在,查看转储数组的“REQUEST_URI”。
我发现这对我不正确,我的app_path似乎是重复的,这导致我回到我的问题的来源,这是我对tests / TestCase.php类进行的修改。
确保$ baseUrl设置正确:
protected $baseUrl = 'http://localhost';
我在尝试修复我的.env文件时遇到了更新,忘了将其更改回来。如果这可以解决您的问题,请告诉我们!
答案 1 :(得分:1)
对于我来说,.env文件中的APP_URL
值应以http://
开头,否则RouteCollection
无法匹配MakesHttpRequest
创建的请求
APP_URL=http://myproject.test
=>有效
APP_URL=myproject.test
=>不起作用
答案 2 :(得分:0)
问题原来是我使用方法Click
,它会点击一个锚标记但我的视图却有一个名为“编辑”的HTML5 button
元素。我将方法切换到Press
并且工作正常。