farhad.a
farhad.a

Reputation: 341

The response is not a view message when unit test fails in laravel

I am new in laravel 5.5 unit testing.

I wrote a method in NewsController names index. It returns a view like below:

public function index(Request $request)
{
     $news = DB::table('news')->orderBy('created_at', 'desc')->get();
     $data['news'] = $news;
     return view('news.index')->with($data);
}

I wrote a test for that like below:

 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);
}

But test failed. The message is:

The response is not a view.

Can anyone help me?

Upvotes: 2

Views: 1494

Answers (1)

Tom Headifen
Tom Headifen

Reputation: 1996

I suspect you are getting some unusual error back which isn't being thrown. There are a few possible things that might be going on. First check your logs and see if there is an error in there. If not then try the following to debug:

In your route it does not appear that you need need to define 'news' with the / as per the docs. https://laravel.com/docs/5.6/routing#route-group-prefixes.

Route::group(['prefix' => 'admin', 'middleware' => 'auth:admin'] , function(){
    Route::get('news' , 'NewsController@index')->name('admin.news.index');
});

If that doesn't work then put dd('here') at the top of your index function and see if your test makes it there. If you are not getting there then check your middleware.

If you are still getting there then dd() your view before you return it.

dd(view('news.index')->with($data))

Check what kind of object that is / what the data in it is.

Ultimately what you are trying to do is follow the logic of what code is being executed and what you are getting back. Hopefully this puts you on the right track.

Upvotes: 1

Related Questions