Adding subdirectory to tests in Laravel; connection can't be resolved

I'm trying to add some structure into my /tests/ directory, at the moment files are structured like this:

  • /tests
  • /Models
  • UserTest.php
  • ExampleTest.php
  • TestCase.php
  • Both UserTest and ExampleTest are extended from TestCase. Running Exampletest works just fine (adds user to database), whereas UserTest fails with the following error:

    Fatal error: Call to a member function connection() on null in [...]IlluminateDatab aseEloquentModel.php on line 3340

    That means that this functions receives null or no argument (model.php:3338-3342):

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

    I cannot figure out what's different in my two testcases.

    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');
        }
    }
    

    Models/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);
        }
    }
    

    Anyone has an idea what's going on here?


    I guess this is happening because as you are extending "TestCase" from Models/UserTest and it is looking "TestCase" inside your Models folder, try giving a full path to that class.

    Something like:

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

    上一篇: PhpStorm中的PHPUnit代码覆盖不会引导Laravel

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