K
K
knowledge2018-10-29 01:23:43
Codeception
knowledge, 2018-10-29 01:23:43

How to test request via codeception in laravel?

there is a username method

public function username()
    {
        $value = request()->input('login');
        $field = filter_var($value, FILTER_VALIDATE_EMAIL) ? 'email' : 'name';

        request()->merge([$field => $value]);

        return $field;
    }

I want to cover it with tests, the test is like this
public function usernameFieldTest(UnitTester $I)
    {
        $name = 'john';
        $email = '[email protected]';

        $loginController = new \App\Http\Controllers\Auth\LoginController();

        $request = new Request();

        $request->merge(['login' => $name]);
        $I->assertEquals('name', $loginController->username());

        $request->merge(['login' => $name]);
        $I->assertEquals('email', $loginController->username());
    }

Error [ReflectionException] Class request does not exist on field $I->assertEquals('name', $loginController->username());
Accordingly, the error is that the test does not find the request () method inside the tested method.
it is impossible to create a request in the username () method through new Request, because it should already exist
. It would be ideal to forward the username(Request $request) request, but this method overrides the method from the laravel vendor library, so this cannot be added

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Tereshchenko, 2018-12-24
@l2ping

Do you have Request $request in the username method of the LoginController class ? Most likely you didn't pass $request to one of the username() parameters .

$I->assertEquals('name', $loginController->username($request));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question