mardi 21 février 2017

Laravel Unit testing Eloquent insertion

I'm currently having some troubles in testing a function in Laravel. This function is a simple save user function.

The current structure involves a User

class User extends Authenticatable

Then I have a UserController

class UserController extends Controller
{
protected $user;

public function __construct(User $user)
{
    $this->user = $user;
    $this->middleware('admins');
}

The save function is defined on the UserController class, this class only assigns the request variables and uses Eloquent save function to save to database.

The function signature is the following:

public function storeUser(NewAccountRequest $request)

The NewAccountRequest object extends from Request and has the validation rules for the request.

class NewAccountRequest extends Request
{
public function authorize()
{
    return true;
}

public function rules()
{
    return [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:user',
        'password' => 'required|min:6|max:60',
    ];
}

}

My problem is how can I unit test this storeUser function.

I have the current test:

public function testSaveUserWithEmptyRequest()
{

    $user = $this->createMock(User::class);
    $controller = new UserController($user);

    $request = $this->createMock(NewAccountRequest::class);
    $store = $controller->saveUser($request);

    $this->assertFalse($store);
}

I'm mocking both User and NewAccountRequest, the problem is that the assertion should be false, from the Eloquent save. Instead I'm getting Null. Any idea on how can I correctly test the function?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2m3F3YH
via IFTTT

Aucun commentaire:

Enregistrer un commentaire