在laravel5.1上发出运行phpunit测试的问题

时间:2016-09-21 19:11:38

标签: testing phpunit laravel-5.1

我正在尝试用laravel 5.1和phpunit测试api,用于登录模块,该模块分为两部分

  • 查看电子邮件地址
  • 获取密码并进行身份验证
  • 现在在第一部分中调用了一个api,它将response.error发送为false,其中以下是编写的测试用例代码。

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

    但是在运行测试表单终端(vendor / bin / phpunit)时,我收到以下错误。

    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.
    

    当我通过邮差测试时,我得到了json格式的所需结果。它与我的代码或phpunit有关吗?

    以下是我正在访问的控制器/路由的源代码

    $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);
    }
    
    下面的

    是堆栈跟踪 - :

        [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 个答案:

    答案 0 :(得分:0)

    看起来您的本地配置没有PDO,这就是运行测试时收到500的原因。

    尝试连接到宅基地并在那里运行测试。这应该可以解决问题。

    将缺少的依赖项安装到本地环境也是一种解决方案。