【发布时间】:2020-10-10 21:00:15
【问题描述】:
我在一个 Laravel 项目中进行了一个测试,在该项目中我执行了一个 getJson 请求,并且应该返回一些答案。但是控制器中的方法没有被命中。
测试
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class NotificationsTest extends TestCase
{
use DatabaseMigrations;
public function setUp(): void
{
parent::setUp();
$this->signIn();
}
public function test_a_user_can_fetch_their_unread_notifications()
{
create(DatabaseNotification::class);
$response = $this->getJson(url('/profiles') . '/' . auth()->user()->name . '/notifications')->json();
$this->assertCount(1, $response);
}
webp.php 中应该处理这个请求的行:
Route::get('/profiles/{user}/notifications', 'UserNotificationsController@index');
用户通知控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\User;
class UserNotificationsController extends Controller
{
public function __construct()
{
$this->middelware('auth');
}
public function index() {
dd(" UsderNotificationsController-Index method hit");
return auth()->user()->unreadNotifications;
}
public function destroy(User $user, $notificationId)
{
dd(' Destroy method hit');
auth()->user()->notifications()->findOrFail($notificationId)->markAsRead();
}
}
如果我用 phpunit 运行测试,我希望 index 方法中的 DD() 应该被执行。但事实并非如此。
我尝试了各种变体来生成 URI,但总是得到相同的结果。谁能告诉我为什么我没有生成正确的 URI?
亲切的问候,
胡贝特
【问题讨论】: