Php Laravel-测试重定向到登录页面,而不是预期的线程页面

Php Laravel-测试重定向到登录页面,而不是预期的线程页面,php,laravel,Php,Laravel,我正在关注Laracasts的视频。我已经创建了一个测试,该测试旨在确保提交的新论坛线程实际得到提交。现在发生的是,当我使用PHPUnit调用$this->get($thread->path())时,测试会重定向到登录页面。然而,有趣的是,我甚至没有设置中间件或任何东西,而且我已经登录,所以我不知道为什么测试会重定向。但是,当我访问浏览器中的线程时。一切正常 这是我的密码 CreateThreadTest.php <?php namespace Tests\Feature; use I

我正在关注Laracasts的视频。我已经创建了一个测试,该测试旨在确保提交的新论坛线程实际得到提交。现在发生的是,当我使用PHPUnit调用
$this->get($thread->path())
时,测试会重定向到登录页面。然而,有趣的是,我甚至没有设置中间件或任何东西,而且我已经登录,所以我不知道为什么测试会重定向。但是,当我访问浏览器中的线程时。一切正常

这是我的密码

CreateThreadTest.php

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class CreateThreadsTest extends TestCase
{

    use DatabaseMigrations;

    function test_an_authenticated_user_can_create_new_forum_threads() {
        $this->ActingAs(factory('App\User')->create());

        $thread = factory('App\Thread')->make();

        $this->post('/forum/threads', $thread->toArray());

        $this->get($thread->path())->assertSee($thread->title)->assertSee($thread->body);


    }
}

您忘记了在测试方法中取回插入的线程,因此它应该如下所示:

 function test_an_authenticated_user_can_create_new_forum_threads() {
        $this->ActingAs(factory('App\User')->create());

        $thread = factory('App\Thread')->make();

        $this->post('/forum/threads', $thread->toArray());

        // you are missing this line

        $threadFromDb = Thread::where($thread->toArray())->first();

        $this->get($threadFromDb->path())->assertSee($thread->title)->assertSee($thread->body);


    }

因为在您的断言中,您使用了
$thread->path()
,而在
path()
方法中,您需要的是未退出的id。

我不久前遵循了相同的系列,并进行了相同的测试

TestCase
类中创建登录函数

受保护的函数登录($user=null)
{
$user=$user?:工厂('App\user')->create();
$this->be($user);
退还$this;
}
然后像这样在测试中使用它

点击响应的URL,而不是不存在的线程

公共功能测试用户可以创建状态()
{
$this->login();
$thread=factory('App\thread')->make();
$response=$this->post('/forum/threads',$thread->toArray());
$this->get($response->headers->get('Location'))
->资产见($thread->title);
->资产见($thread->body);
}
您可能会在GitHub回购协议上发现一些更有趣的调整


希望这能有所帮助

你的回答肯定为我指明了正确的方向。我
dd($threadFromDb)
发现我的线程甚至没有保存在数据库中,不知道为什么though@TomMorison请把答案投上去,因为它对你有帮助!从错误中,我可以看到它不是登录页面,它实际上是主页,但现在您已经知道了原因;)
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Thread extends Model
{
    protected $guarded = [];
    public function path() {
        return "/forum/threads/" . $this->id;
    }

    public function replies() {
        return $this->hasMany(Reply::class);
    }

    public function creator()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function addReply($reply) {
        $this->replies()->create($reply);
    }
}


Time: 866 ms, Memory: 26.00 MB

There was 1 failure:

1) Tests\Feature\CreateThreadsTest::test_an_authenticated_user_can_create_new_forum_threads
Failed asserting that '<!DOCTYPE html>\n
<html lang="en">\n
<head>\n
    <meta charset="utf-8">\n
    <meta name="viewport" content="width=device-width, initial-scale=1">\n
\n
    <!-- CSRF Token -->\n
    <meta name="csrf-token" content="aVe2XAPPH8ZUdPONN1Teqg1Wb7fgE7cPqhfSwl9E">\n
\n
    <title>Laravel</title>\n
\n
    <!-- Scripts -->\n
    <script src="http://localhost/js/app.js" defer></script>\n
\n
    <!-- Fonts -->\n
    <link rel="dns-prefetch" href="//fonts.gstatic.com">\n
    <link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">\n
\n
    <!-- Styles -->\n
    <link href="http://localhost/css/app.css" rel="stylesheet">\n
</head>\n
<body style="padding-bottom:100px">\n
    <div id="app">\n
        <nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm">\n
            <div class="container">\n
                <a class="navbar-brand" href="http://localhost">\n
                    Laravel\n
                </a>\n
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">\n
                    <span class="navbar-toggler-icon"></span>\n
                </button>\n
\n
                <div class="collapse navbar-collapse" id="navbarSupportedContent">\n
                    <!-- Left Side Of Navbar -->\n
                    <ul class="navbar-nav mr-auto">\n
                        <li><a href="/forum/threads">All threads</a></li>\n
                        <li></li>\n
                    </ul>\n
\n
                    <!-- Right Side Of Navbar -->\n
                    <ul class="navbar-nav ml-auto">\n
                        <!-- Authentication Links -->\n
                                                    <li class="nav-item dropdown">\n
                                <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>\n
                                    Mr. Magnus Fay III <span class="caret"></span>\n
                                </a>\n
\n
                                <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">\n
                                    <a class="dropdown-item" href="http://localhost/logout"\n
                                       onclick="event.preventDefault();\n
                                                     document.getElementById('logout-form').submit();">\n
                                        Logout\n
                                    </a>\n
\n
                                    <form id="logout-form" action="http://localhost/logout" method="POST" style="display: none;">\n
                                        <input type="hidden" name="_token" value="aVe2XAPPH8ZUdPONN1Teqg1Wb7fgE7cPqhfSwl9E">                                    </form>\n
                                </div>\n
                            </li>\n
                                            </ul>\n
                </div>\n
            </div>\n
        </nav>\n
\n
        <main class="py-4">\n
                <div class="container">\n
        <div class="row justify-content-center">\n
            <div class="col-md-8">\n
                <div class="card">\n
                    <div class="card-header">Forum threads</div>\n
\n
                    <div class="card-body">\n
                        \n
\n
                        You are logged in!\n
                    </div>\n
                </div>\n
            </div>\n
        </div>\n
    </div>\n
        </main>\n
    </div>\n
</body>\n
</html>\n
' contains "Ut neque nam maxime facilis sunt et.".

/home/vagrant/Code/intransportal/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:363
/home/vagrant/Code/intransportal/tests/Feature/CreateThreadsTest.php:22
/home/vagrant/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:200
/home/vagrant/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:159

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
 function test_an_authenticated_user_can_create_new_forum_threads() {
        $this->ActingAs(factory('App\User')->create());

        $thread = factory('App\Thread')->make();

        $this->post('/forum/threads', $thread->toArray());

        // you are missing this line

        $threadFromDb = Thread::where($thread->toArray())->first();

        $this->get($threadFromDb->path())->assertSee($thread->title)->assertSee($thread->body);


    }