【发布时间】: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