Laravel 测试:调用未定义的函数 App\Http\Controllers\get()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45864564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Testing : Call to undefined function App\Http\Controllers\get()
提问by Meteor Newbie
I am going through a Laravel tutorial and stuck at a "Call to undefined function" error. So far I have 20 Tests with 28 Assertions and only this test fails. I can't find my typo. Tell me please what source code do I have to add additionaly. I am new to Laravel.
我正在阅读 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.
There must be a typo, but I can't find it.
一定有错别字,但我找不到。
ReadThreadsTest.php
读取线程测试.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
网页.php
Route::get('threads/{channel}', 'ThreadsController@index');
Channel.php
频道.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
线程控制器.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
<?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);
}
}
回答by Meteor Newbie
I found finaly my typo after few hours searching ...
经过几个小时的搜索,我终于找到了我的错字......
this:
这个:
$threads = $channel->threads()->latest()-get();
must be:
必须是:
$threads = $channel->threads()->latest()->get();
Now everything works.
现在一切正常。