farhadkb6868's avatar

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

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?

0 likes
4 replies
fahadkhan1740's avatar

what is the purpose of making a News factory when you are not using it anywhere?

Also, store the response like this $response = $this->get('/admin/news'). And then do assertions on the $reponse mentioned below.

$response->assertStatus(200)->assertSee($value);
iraklisg's avatar

@farhadkb6868 it is possible that the problem has to do with the fact that when you try to send a GET request to /admin/news, an exception is thrown and and passed to the TesResponse instance ($response), instead of the actual View object:

$response = $this->get('/admin/news') // some exception thrown during this call and passed to the response
$response->assertViewHas('news', $news)  // response is not a view, that's why the "response is not a view" error

I would suggest to try disabling the Laravel's default exception handling to get better error output

1 like
jminklerdmopc's avatar

Sounds like you don't have permission in your test if you are in admin, you need to call the get() method actingAs($admin)

Please or to participate in this conversation.