将子目录添加到Laravel的测试中; 连接无法解决

我试图在我的/ tests /目录中添加一些结构,目前文件的结构如下所示:

  • /测试
  • /楷模
  • UserTest.php
  • ExampleTest.php
  • TestCase.php
  • UserTest和ExampleTest都是从TestCase扩展而来的。 运行Exampletest工作得很好(将用户添加到数据库),而UserTest失败并出现以下错误:

    致命错误:调用第3340行的 Illuminate Datab ase Eloquent Model.php中的null成员函数connection()

    这意味着这个函数接收null或没有参数(model.php:3338-3342):

    public static function resolveConnection($connection = null)
    {
        return static::$resolver->connection($connection);
    }
    

    我无法弄清楚我的两个测试用例有什么不同。

    TestCase.php:

    <?php
    namespace Tests;
    
    use IlluminateFoundationTestingTestCase as baseTestCase;
    
    class TestCase extends baseTestCase
    {
        /**
         * The base URL to use while testing the application.
         *
         * @var string
         */
        protected $baseUrl = 'http://localhost';
    
        /**
         * Creates the application.
         *
         * @return IlluminateFoundationApplication
         */
        public function createApplication()
        {
            $app = require __DIR__ . '/../bootstrap/app.php';
    
            $app->make(IlluminateContractsConsoleKernel::class)->bootstrap();
    
            return $app;
        }
    }
    

    ExampleTest.php:

    <?php
    
    use IlluminateFoundationTestingWithoutMiddleware;
    use IlluminateFoundationTestingDatabaseMigrations;
    use IlluminateFoundationTestingDatabaseTransactions;
    use TestsTestCase;
    use AppUser;
    
    class ExampleTest extends TestCase
    {
        /**
         * A basic functional test example.
         *
         * @return void
         */
        public function testBasicExample()
        {
            User::create([
                "name" => 'test'
            ]);
    
            $this->visit('/')
                 ->see('Laravel 5');
        }
    }
    

    型号/ UserTest.php:

    <?php
    
    namespace TestsModels;
    
    use IlluminateFoundationTestingDatabaseTransactions;
    use IlluminateSupportFacadesHash;
    use TestsTestCase;
    use FakerFactory;
    use AppUser;
    
    class UserTest extends TestCase
    {
        use DatabaseTransactions;
    
        protected $user;
    
        protected $password;
    
        protected $customPassword;
    
        protected $testString;
        /**
         * Construct.
         *
         * @return void
         */
    
        public function __construct()
        {
            $faker = Factory::create();
    
            $this->password = $faker->password;
            $this->customPassword = $faker->password;
    
            $this->user = User::create([
                "name" => $faker->name,
                "email" => $faker->email,
                "password" => Hash::shouldReceive($this->password),
            ]);
    
        }
    
        public function testExample()
        {
            $this->assertTrue(true);
        }
    }
    

    任何人都知道这里发生了什么?


    我想这是因为你正在从Models / UserTest中扩展“TestCase”,并且它正在Model文件夹中查找“TestCase”,请尝试给出该​​类的完整路径。

    就像是:

    class UserTest extends {yourNameSpace(ifAny)}TestCase
    
    链接地址: http://www.djcxy.com/p/83199.html

    上一篇: Adding subdirectory to tests in Laravel; connection can't be resolved

    下一篇: Laravel 4.2 custom Observer Event