0
votes

I Am trying to test api with laravel 5.1 and phpunit for a login module which is divided into 2 parts

  • Check email address
  • get password and authenticate
  • Now in the first part an api is called which sends the response.error as false for which the follow is the test case code written.

    class LoginPageTest extends TestCase
    {
    use WithoutMiddleware;
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
    
    $response = $this->call('POST', '/check-user', ['email' => '[email protected]']);
    
        $this->assertEquals(200, $response->status());
    
        }
    }
    

    But on running the test form the terminal(vendor/bin/phpunit), I get the following error.

    Time: 947 ms, Memory: 14.75MB
    
    There was 1 failure:
    
    1) LoginPageTest::testExample
    Failed asserting that 500 matches expected 200.
    
    project/tests/LoginPageTest.php:32
    
    FAILURES!
    Tests: 1, Assertions: 1, Failures: 1.
    

    When i test thru postman i get the desired result in the json format. Is it something to do with my code or phpunit?

    The following is the source code of the controller/route i am accessing

    $user_email = $request->get('email');
    $user = $this->_user->findByField(
            'email', $user_email,
            ['email', 'is_active']
        )->first();
    if (! empty($user)) {
        $response = view('auth/password', compact('user_email'))->render();
    
        return $this->sendSuccess('proceed for password', $response, 200);
    }
    

    below is the stack trace-:

        [2016-09-26 19:58:48] testing.ERROR: exception 'PDOException' with message 'could not find driver' in /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:55
    Stack trace:
    #0 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(55): PDO->__construct('pgsql:host=127....', 'homestead', 'secret', Array)
    #1 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Database/Connectors/PostgresConnector.php(36): Illuminate\Database\Connectors\Connector->createConnection('pgsql:host=127....', Array, Array)
    #2 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php(60): Illuminate\Database\Connectors\PostgresConnector->connect(Array)
    #3 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php(49): Illuminate\Database\Connectors\ConnectionFactory->createSingleConnection(Array)
    #4 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php(175): Illuminate\Database\Connectors\ConnectionFactory->make(Array, 'pgsql')
    #5 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php(67): Illuminate\Database\DatabaseManager->makeConnection('pgsql')
    #6 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(3224): Illuminate\Database\DatabaseManager->connection(NULL)
    #7 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(3190): Illuminate\Database\Eloquent\Model::resolveConnection(NULL)
    #8 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1870): Illuminate\Database\Eloquent\Model->getConnection()
    #9 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1813): Illuminate\Database\Eloquent\Model->newBaseQueryBuilder()
    #10 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1787): Illuminate\Database\Eloquent\Model->newQueryWithoutScopes()
    #11 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(3442): Illuminate\Database\Eloquent\Model->newQuery()
    #12 /home/hireajackal/work/hireajackal/vendor/prettus/l5-repository/src/Prettus/Repository/Eloquent/BaseRepository.php(379): Illuminate\Database\Eloquent\Model->__call('where', Array)
    #13 /home/hireajackal/work/hireajackal/vendor/prettus/l5-repository/src/Prettus/Repository/Eloquent/BaseRepository.php(379): App\User->where('email', '=', 'sudhir.kadam@hi...')
    #14 /home/hireajackal/work/hireajackal/app/Http/Controllers/Auth/AuthController.php(103): Prettus\Repository\Eloquent\BaseRepository->findByField('email', 'sudhir.kadam@hi...', Array)
    #15 [internal function]: App\Http\Controllers\Auth\AuthController->getUser(Object(App\Http\Requests\Validations\GetUserRequest))
    #16 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(256): call_user_func_array(Array, Array)
    #17 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(164): Illuminate\Routing\Controller->callAction('getUser', Array)
    #18 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(112): Illuminate\Routing\ControllerDispatcher->call(Object(App\Http\Controllers\Auth\AuthController), Object(Illuminate\Routing\Route), 'getUser')
    #19 [internal function]: Illuminate\Routing\ControllerDispatcher->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
    #20 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(139): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
    #21 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #22 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
    #23 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(114): Illuminate\Pipeline\Pipeline->then(Object(Closure))
    #24 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(69): Illuminate\Routing\ControllerDispatcher->callWithinStack(Object(App\Http\Controllers\Auth\AuthController), Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'getUser')
    #25 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Routing/Route.php(203): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'App\Http\Contro...', 'getUser')
    #26 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Routing/Route.php(134): Illuminate\Routing\Route->runWithCustomDispatcher(Object(Illuminate\Http\Request))
    #27 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Routing/Router.php(708): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
    #28 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
    #29 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(139): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
    #30 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #31 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
    #32 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Routing/Router.php(710): Illuminate\Pipeline\Pipeline->then(Object(Closure))
    #33 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Routing/Router.php(675): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
    #34 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Routing/Router.php(635): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
    #35 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(236): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
    #36 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
    #37 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(139): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
    #38 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #39 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
    #40 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(122): Illuminate\Pipeline\Pipeline->then(Object(Closure))
    #41 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(87): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
    #42 /home/hireajackal/work/hireajackal/vendor/laravel/framework/src/Illuminate/Foundation/Testing/CrawlerTrait.php(394): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
    #43 /home/hireajackal/work/hireajackal/tests/LoginPageTest.php(30): Illuminate\Foundation\Testing\TestCase->call('POST', '/check-user', Array)
    #44 [internal function]: LoginPageTest->testExample()
    #45 /home/hireajackal/work/hireajackal/vendor/phpunit/phpunit/src/Framework/TestCase.php(909): ReflectionMethod->invokeArgs(Object(LoginPageTest), Array)
    #46 /home/hireajackal/work/hireajackal/vendor/phpunit/phpunit/src/Framework/TestCase.php(768): PHPUnit_Framework_TestCase->runTest()
    #47 /home/hireajackal/work/hireajackal/vendor/phpunit/phpunit/src/Framework/TestResult.php(612): PHPUnit_Framework_TestCase->runBare()
    #48 /home/hireajackal/work/hireajackal/vendor/phpunit/phpunit/src/Framework/TestCase.php(724): PHPUnit_Framework_TestResult->run(Object(LoginPageTest))
    #49 /home/hireajackal/work/hireajackal/vendor/phpunit/phpunit/src/Framework/TestSuite.php(747): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
    #50 /home/hireajackal/work/hireajackal/vendor/phpunit/phpunit/src/Framework/TestSuite.php(747): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
    #51 /home/hireajackal/work/hireajackal/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(440): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
    #52 /home/hireajackal/work/hireajackal/vendor/phpunit/phpunit/src/TextUI/Command.php(149): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
    #53 /home/hireajackal/work/hireajackal/vendor/phpunit/phpunit/src/TextUI/Command.php(100): PHPUnit_TextUI_Command->run(Array, true)
    #54 /home/hireajackal/work/hireajackal/vendor/phpunit/phpunit/phpunit(47): PHPUnit_TextUI_Command::main()
    #55 {main}  
    
    1
    Do you mind sharing the code covered by this test case and the exception log?Hilmi Erdem KEREN
    I think you didn't get what I mean. Can you update your question and add the controller or route source being tested? aka '/check-user'Hilmi Erdem KEREN
    Pls check above i have included the source code from the route controller. On testing this thru postman i get a successful response of 200.Amit Kumar
    It looks like you are having trouble inside _user class. Can you please share the stacktrace generated by this action? (probably under storage/logs/laravel-date-.log)Hilmi Erdem KEREN
    Please check above i have added stack trace.Amit Kumar

    1 Answers

    0
    votes

    Looks like your local configuration doesn't have PDO and that is why you receive 500 when running tests.

    Try connecting to homestead and running your tests there. That should do the trick.

    Installing missing dependencies to local environment is also a solution.