What is this unit test intended to achieve? Why not just make a feature test hitting the endpoint with valid data, and assert against the TestResponse?
Jan 30, 2023
12
Level 1
Argument 1 passed to App\Http\Controllers\LibraryController::TagParser() must be an instance of Illuminate\Http\Request, string given,
I am haveing trouble resolving this error I am getting for a unit test. The last ex of the Controller is what the full code will look like. However, I wanted take it a step at a time. I was able to get the test to pass using ($request) only in the controller. However I want to replace the function in the controller with what I have written in EXample.1 below. Whenever I run the unit test with EXample.1 I get the error directly following EXample.1 below:
EXample 1 : public function TagParser(Request $request)
There was 1 error:
1) Tests\UserControllerTest::test_it_gets_a_list_of_params
TypeError: Argument 1 passed to App\Http\Controllers\UserController::TagParser() must be an instance of Illuminate\Http\Request, string given, called in /PHP/tests/TagParserTest.php on line 22
Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class UserController extends Controller
{
public function TagParser($request)
{
return [$request];
}
TagParserTest.php
<?php
namespace Tests;
use App\Http\Controllers\UserController;
use Illuminate\Http\Request;
use PHPUnit\Framework\TestCase;
class UserControllerTest extends TestCase
{
public function test_it_gets_a_list_of_params()
{
$parser = new UserController;
$result = $parser->TagParser("personal");
$expected = ['personal'];
$expected should match $result
$this->assertSame($expected, $result);
}
}
What am I missing from my TagParserTest to make the test run with the following:
Updated Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Services\UserService;
use DB;
class UserController extends Controller
{
public function TagParser(Request $request)
{
$username = $request->get('username');
$currentLabel = $request->get('previousLabel');
$newLabel = $request->get('updatedLabel');
$currentNum = $request->get('previousNum);
$newNum = $request->get('updatedNum');
$UserService = new UserEditorService();
$UserService->updateUser($username, $currentLabel, $newLabel, $currentNum, $newNum);
return ['message' => 'User has been updated successfully ', 'status' => 200];
}
Please or to participate in this conversation.