Php 当单元测试在laravel中失败时,响应不是视图消息

Php 当单元测试在laravel中失败时,响应不是视图消息,php,phpunit,laravel-5.5,Php,Phpunit,Laravel 5.5,我是laravel 5.5单元测试的新手 我在newcontrollernames索引中编写了一个方法。它返回如下视图: public function index(Request $request) { $news = DB::table('news')->orderBy('created_at', 'desc')->get(); $data['news'] = $news; return view('news.index')->with($da

我是laravel 5.5单元测试的新手

我在
newcontroller
names索引中编写了一个方法。它返回如下视图:

public function index(Request $request)
{
     $news = DB::table('news')->orderBy('created_at', 'desc')->get();
     $data['news'] = $news;
     return view('news.index')->with($data);
}
 public function testIndex(){
     factory(News::class, 10)->create();
    $news = DB::table('news')->orderBy('created_at', 'desc')->get();
    $this->get('/admin/news')
       ->assertViewHas('news', $news)
       ->assertStatus(200);
}
我为此编写了一个测试,如下所示:

public function index(Request $request)
{
     $news = DB::table('news')->orderBy('created_at', 'desc')->get();
     $data['news'] = $news;
     return view('news.index')->with($data);
}
 public function testIndex(){
     factory(News::class, 10)->create();
    $news = DB::table('news')->orderBy('created_at', 'desc')->get();
    $this->get('/admin/news')
       ->assertViewHas('news', $news)
       ->assertStatus(200);
}
但测试失败了。信息是:

响应不是视图。


有人能帮我吗?

我怀疑你得到了一些不寻常的错误,但没有被抛出。可能会发生一些事情。首先检查您的日志,看看其中是否有错误。如果没有,请尝试以下方法进行调试:

在您的路线中,似乎不需要根据文档使用
/
定义“新闻”

如果这不起作用,那么将
dd('here')
放在索引函数的顶部,看看测试是否成功。如果你还没有到达那里,那么检查你的中间件

如果您仍在返回视图之前,请先查看视图

dd(查看('news.index')->带($data))

检查这是什么类型的对象/其中的数据是什么


最终,您要做的是遵循执行的代码和返回的代码的逻辑。希望这能让你走上正轨。

你能发布你的web.php代码吗?Route::group(['prefix'=>'admin','middleware'=>'auth:admin',function(){Route::get('/news','NewsController@index)->name('admin.news.index');});