You pass in the wrong variable. This
$response = $this->userController->update($update, $request->id);
Should be this
$response = $this->userController->update($request, $request->id);
At least, it seems like that ;)
Hello.
Today I started wih the unit test in my repository pattern and everything it's ok instead one method: update the information.
UserTest.php
<?php
use App\User;
use App\Http\Controllers\UserController;
use App\Http\Requests\UserRequest;
use App\Repositories\UserRepository;
use App\Services\UserService;
use Illuminate\Support\Facades\Event;
use Illuminate\Http\Request;
class UserRules
{
public static $rules = array(
'name' => 'required'
);
}
class UserTest extends TestCase
{
public $userController;
public function __construct()
{
$userModel = new User();
$userRepository = new UserRepository($userModel);
$userService = new UserService($userRepository);
$this->userController = new UserController($userService);
}
public function testGetAll()
{
Event::fake();
$response = $this->userController->getServices();
$this->assertEquals(200, $response->getStatusCode());
}
public function testCreate()
{
Event::fake();
$request = UserRequest::create(route('users.create'), 'POST',[
'name'=>'fake name',
'surname'=>'fake surname',
'address'=>'fake address'
]);
$response = $this->userController->store($request);
$this->assertEquals(302, $response->getStatusCode());
}
public function testUpdate()
{
$request = UserRequest::create(route('users.create'), 'POST',[
'name'=>'fake name',
'surname'=>'fake surname',
'address'=>'fake address'
]);
$update = $this->json('PUT', route('users.edit', $request->id),[
'name'=>$request->name.'_updated'
]);
$response = $this->userController->update($update, $request->id);
$this->assertEquals(200, $response->getStatusCode());
}
public function testDelete()
{
Event::fake();
$request = Request::create(route('users.delete'), 'POST', ['id'=>63]);
$response = $this->userController->destroy($request);
$this->assertEquals(200, $response->getStatusCode());
}
public function testUserWithoutName()
{
$user = new User();
$validator = Validator::make(array('address' => 'fake address'), UserRules::$rules);
$this->assertFalse($validator->passes());
}
public function testUserWithName()
{
$user = new User();
$validator = Validator::make(array('name' => 'fake name'), UserRules::$rules);
$this->assertTrue($validator->passes());
}
}
UserRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required'
];
}
}
When I trying to update, the error that appears is:
TypeError: Argument 1 passed to App\Http\Controllers\UserController::update() must be an instance of App\Http\Requests\UserRequest, instance of UserTest given, called in C:\Users\XXXXX\tests\UserTest.php on line 79
I think that's ok, but I don't see where must be the error :/
You pass in the wrong variable. This
$response = $this->userController->update($update, $request->id);
Should be this
$response = $this->userController->update($request, $request->id);
At least, it seems like that ;)
Please or to participate in this conversation.