【问题标题】:Adding subdirectory to tests in Laravel; connection can't be resolved在 Laravel 中的测试中添加子目录;无法解决连接
【发布时间】:2016-05-26 21:16:51
【问题描述】:

我正在尝试将一些结构添加到我的 /tests/ 目录中,目前文件的结构如下:

  • /测试
    • /型号
      • UserTest.php
    • ExampleTest.php
    • TestCase.php

UserTest 和 ExampleTest 都是从 TestCase 扩展而来的。运行 Exampletest 工作正常(将用户添加到数据库),而 UserTest 失败并出现以下错误:

致命错误:在第 3340 行 [...]\Illuminate\Database\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 Illuminate\Foundation\Testing\TestCase 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 \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        $app = require __DIR__ . '/../bootstrap/app.php';

        $app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();

        return $app;
    }
}

ExampleTest.php:

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
use App\User;

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 Tests\Models;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;
use Faker\Factory;
use App\User;

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

有人知道这里发生了什么吗?

【问题讨论】:

    标签: php laravel-5 namespaces phpunit


    【解决方案1】:

    我猜这是因为当您从 Models/UserTest 扩展“TestCase”并且它在您的 Models 文件夹中查找“TestCase”时,请尝试提供该类的完整路径。

    类似:

    class UserTest extends {yourNameSpace(ifAny)}\TestCase
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-12
      • 2018-05-19
      • 2020-06-18
      • 2012-02-21
      • 2017-08-02
      • 2018-07-15
      相关资源
      最近更新 更多