How to do unit testing of the controller's function which returns a view. Here, is my controller function
public function index(Request $request)
{
$data = Data::all();
return view('index',[
'data' => $data
]);
}
Here is test code
public function testIndex()
{
$response = $this->withoutMiddleware();
$response = $this->get('/user');
$response->assertSuccessful();
}
I tried this
public function testIndex()
{
$response = $this->withoutMiddleware();
$response = $this->get('/user');
$response = $this->assertContains('data', $response->content());
$response->assertSuccessful();
}
which shows error of
Error: Call to a member function assertSuccessful() on null
Any idea, How to write a test case for my index controller function?
You reassigned $response in your first assert. No need to do that
I don't know what assertContains returns off the top of my head, but I bet it's not $this.
Honestly, I don't know why the sample has you assigning anything to $response in the first place except the response from an HTTP query.
public function testIndex()
{
$this->withoutMiddleware();
$response = $this->get('/user');
$this->assertContains('data', $response->content());
$response->assertSuccessful();
}
You can use too
$response->assertSeeText('data');
您可以使用此代码
$this->get('/user')->assertViewHas('data');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.