【问题标题】:Laravel Testing : Call to undefined function App\Http\Controllers\get()Laravel 测试:调用未定义的函数 App\Http\Controllers\get()
【发布时间】:2018-02-02 12:14:44
【问题描述】:

我正在阅读 Laravel 教程并遇到“调用未定义函数”错误。到目前为止,我有 20 个测试和 28 个断言,只有这个测试失败。我找不到我的错字。请告诉我我必须添加哪些源代码。我是 Laravel 的新手。

λ vendor\bin\phpunit --filter a_user_can_filter_threads_according_to_a_channel
PHPUnit 5.7.21 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 409 ms, Memory: 14.00MB

There was 1 error:

1) Tests\Feature\ReadThreadsTest::a_user_can_filter_threads_according_to_a_channel
Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined function App\Http\Controllers\get()

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

肯定有错字,但我找不到。

ReadThreadsT​​est.php

/** @test */
public function a_user_can_filter_threads_according_to_a_channel()
{
    $channel = create('App\Channel');
    $threadInChannel = create('App\Thread', ['channel_id' => $channel->id]);
    $threadNotInChannel = create('App\Thread');

    $this->get('/threads/' . $channel->slug)
        ->assertSee($threadInChannel->title)
        ->assertDontSee($threadNotInChannel->title);
}

web.php

Route::get('threads/{channel}', 'ThreadsController@index');

Channel.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Channel extends Model
{
    public function getRouteKeyName()
    {
        return 'slug';
    }

    public function threads()
    {
        return $this->hasMany(Thread::class);
    }

}

ThreadController.php

public function index(Channel $channel)
{

    if ($channel->exists)
    {
        $threads = $channel->threads()->latest()-get();
    } else {
        $threads = Thread::latest()->get();
    }

    return view('threads.index', compact('threads'));
} 

Thread.php

<?php


namespace App;

use Illuminate\Database\Eloquent\Model;


class Thread extends Model
{

    protected $guarded = [];

    public function path()
    {
        return "/threads/{$this->channel->slug}/{$this->id}";
    }

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

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

    public function channel()
    {
        return $this->belongsTo(Channel::class);
    }

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

【问题讨论】:

  • 你能展示你完整的 ReadThreadsT​​est 课程吗?
  • $this-&gt;get('/threads/' . $channel-&gt;slug) 不工作;如果您尝试在该 URL 上调用 GET 请求,则必须使用 HTTP 客户端,例如 Guzzle
  • 我终于找到了我的错字。缺少此令牌“>”:-&gt;latest()-get() 无论如何谢谢!

标签: laravel laravel-5 laravel-testing


【解决方案1】:

经过几个小时的搜索,我终于找到了我的错字...

这个:

    $threads = $channel->threads()->latest()-get();

必须是:

    $threads = $channel->threads()->latest()->get();

现在一切正常。

【讨论】:

  • 如果解决方案是纠正错字,只需删除问题。
  • 我收到此消息:“删除已回答的问题可能会导致您的帐户被阻止提问。您确定要删除吗?”我不知道这只是一个错字还是教程中的这个例子不适用于我的 laravel 版本。 Laravel 没有显示语法错误或其他错误来帮助我找到这个错字。
猜你喜欢
  • 2017-10-16
  • 2018-10-16
  • 1970-01-01
  • 2020-01-10
  • 2020-02-01
  • 1970-01-01
  • 2022-11-15
  • 2018-08-24
  • 2019-11-17
相关资源
最近更新 更多