测试工匠司令部Laravel 9

发布于 2025-02-03 04:34:43 字数 356 浏览 3 评论 0原文

我正在尝试在Laravel 9中编写一个测试命令。在以下代码中,新用户将成功创建,但是在创建仪表板页面之后,如何

public function test_an_action_that_requires_authentication()
{
    $user = $this->artisan('make:user',[
        'name' => "username",
        'email' => "useremail",
        'password' => Str::random(8)
    ]);
}

在成功创建成功之后如何重定向到路线

I'm trying to write a test command in Laravel 9. In the below code the new user will create successfully But after creating need to redirect the Dashboard page

public function test_an_action_that_requires_authentication()
{
    $user = $this->artisan('make:user',[
        'name' => "username",
        'email' => "useremail",
        'password' => Str::random(8)
    ]);
}

How to redirect to the route after success created

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈场末日恋爱 2025-02-10 04:34:43

我们将使用assertexitCode方法来断言命令是用给定的退出代码完成的。

$this->artisan('module:import')
    ->expectsConfirmation('Do you really wish to run this command?', 'no')
    ->assertExitCode(1);

有关更多信息,您可以在此处找到

https://lararavel.com/docs/9.x /控制台检验

We will use the assertExitCode method to assert that the command completed with a given exit code.

$this->artisan('module:import')
    ->expectsConfirmation('Do you really wish to run this command?', 'no')
    ->assertExitCode(1);

for more information you can find here

https://laravel.com/docs/9.x/console-tests

染墨丶若流云 2025-02-10 04:34:43

NB此代码在测试中不起作用。您无法重定向工匠司令部。

说,在Laravel中,要重定向到主页,您可以在控制器函数中使用此代码:

return redirect(url('/'));

N.B. This code does not work in test. You can not redirect in an artisan command.

Said that, in laravel, to redirect to the home page, you can use this code in a controller function:

return redirect(url('/'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文