我正在学习 Laravel 测试,但由于以下错误,我无法继续测试 Eloquent:
错误:对空值调用成员函数connection()
这是我的代码:
public function test_users_can_follow_each_other()
{
$this->userOne = User::find(1);
$this->userTwo = User::find(2);
$this->userOne->followedBy($this->userTwo->id);
$this->assertTrue($this->userTwo->isFollowedBy($this->userOne->id));
}
Run Code Online (Sandbox Code Playgroud)
我也在使用这个特质use DatabaseTransactions;
数据库已创建,用户表上有 2 条记录。还需要配置什么吗,谢谢!
我正在尝试编写一个 Laravel PHPUnit 测试来检查创建用户后邮件是否已排队。
<?php
namespace Tests\Unit\User;
use App\User;
use Tests\TestCase;
use App\Notifications\UserCreated;
use Illuminate\Support\Facades\Mail;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Notification;
use Illuminate\Foundation\Testing\RefreshDatabase;
class UserUnitTest extends TestCase
{
use RefreshDatabase;
/**
* check if a user was created in database
*
* @return void
*/
public function testUserCreate()
{
$user = factory(User::class)->create();
$this->assertDatabaseHas('users', [
'email' => $user->email,
'active' => 0,
'activation_token' => $user->activation_token,
'deleted_at' => NULL
]);
}
/**
* check if email was sent after user was created in database
* …Run Code Online (Sandbox Code Playgroud) 我想在内存中创建一个新的 sqlite 测试数据库,以便每次运行 bin/phpunit 时都运行测试。到目前为止我找到的解决方案清空了测试数据库,但是当我添加灯具时,它们的 id 从擦除之前的最后一个自动增量开始。
我创建了一个 maatwebsite/excel 导入例程,我想测试它。
maatwebsite/excel 测试页面除了伪造之外没有向我提供任何其他信息。但我需要上传我的真实 Excel 文件,因为我想验证 Excel 文件中的数据是否已正确处理。
这是我的上传输入字段和用于点击端点的相应按钮/import
<form action="/import" method="post" enctype="multipart/form-data">
@csrf
<div class="form-group">
<input type="file" class="form-control-file file-path" name="fileToUpload">
</div>
<button type="submit" class="btn btn-primary">Import File</button>
</form>
Run Code Online (Sandbox Code Playgroud)
在视图的控制器端,将处理并导入上传的文件。
...
public function store(Request $request) {
$request->validate([
'fileToUpload' => 'required|file|max:4096|mimes:xls,xlsx',
]);
// start the import
Excel::import(new SheetNavigator, request()->file('fileToUpload'));
...
Run Code Online (Sandbox Code Playgroud)
需要导入的文件位于我的测试环境中:
/tests
/files
/myexcel.xlsx
Run Code Online (Sandbox Code Playgroud)
public function test_user_can_import_file() {
Excel::fake();
$datafile = new UploadedFile(
base_path('tests/files/myfile.xlsx'),
'myfile.xlsx',
'xlsx',
13071,
true);
$res = $this->post('/import', [
'fileToUpload' => $datafile
]);
// asserting, that everything …Run Code Online (Sandbox Code Playgroud) 我为我的新 Laravel 7 应用程序创建了一个简单的测试。但是当我运行时php artisan test出现以下错误。
Target [Illuminate\Contracts\View\Factory] is not instantiable.
当我在浏览器中转到该页面时,不会出现该错误。
$controller = new HomeController();
$request = Request::create('/', 'GET');
$response = $controller->home($request);
$this->assertEquals(200, $response->getStatusCode());
Run Code Online (Sandbox Code Playgroud) 我正在尝试设置一个数据库环境来测试我的 Doctrine ORM 实体类,而不更改我的真实数据库。
我遵循了Symfony 的文档,但是当我运行时php bin/console doctrine:fixtures:load我收到警告:
小心,数据库“图表”将被清除。你想继续吗?(是/否)[否]:
尽管我已经在我的.env.test文件中设置了一个环境变量:
DATABASE_URL=mysql://testUser:testPassword@127.0.0.1/graph_test
编辑:这是整个.env.test文件:
# define your env variables for the test env here
KERNEL_CLASS='App\Kernel'
APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther
# Database test
DATABASE_URL=mysql://testUser:testPassword@127.0.0.1/graph_test
Run Code Online (Sandbox Code Playgroud)
我尝试更改我的test/bootstrap.php文件,但收到相同的警告。
DATABASE_URL有没有办法告诉原则从文件中获取值.env.test?任何帮助将不胜感激。
PS 不需要多个测试 .env 文件。
当我在 laravel 中使用 PHPUnit 并在运行 PHPUnit 后将工厂添加到我自己的测试类时,控制台中会出现以下错误:
SQLSTATE[HY000]: General error: 1 no such table: users
Run Code Online (Sandbox Code Playgroud)
我的测试类方法:
public function testExistSomeTextsInIndexPage()
{
$users= factory(User::class)->create();
$this->get('/')->assertSee($users->name);
}
Run Code Online (Sandbox Code Playgroud)
该工厂代码在我的项目的其他部分正常工作,只是在测试类中显示错误
我使用此命令对用户模块运行测试
./vendor/bin/paratest -pauto modules/User
Run Code Online (Sandbox Code Playgroud)
运行 module/User/Tests 文件夹中的每个测试
我想在模块/用户/测试文件中的特定文件中运行特定函数
路径为:modules/User/Tests/UserTest.php
我要测试的功能是:
function testUserSearch() { }
Run Code Online (Sandbox Code Playgroud)
我只想测试 testUserSearch
有没有办法使用 paratest 来做到这一点?
我收到此错误:
\n\n\n调用未定义的方法 Tests\\Feature\\ExampleTest::visit()
\n
在运行我的测试用例时。我对 TDD 有点陌生。
\n这是我的示例测试代码
\n<?php\n\nnamespace Tests\\Feature;\n\nuse Illuminate\\Foundation\\Testing\\RefreshDatabase;\nuse Tests\\TestCase;\n\nclass ExampleTest extends TestCase\n{\n\n /**\n * A basic test example.\n *\n * @return void\n */\n\n public function test_example()\n {\n $response = $this->visit('/')->see('Laravel');\n $response->assertStatus(200);\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n从我用来了解 TDD 的视频教程中,上面的代码运行良好,没有任何问题,但是当我在我这边运行代码时,我遇到了如下错误:
\n\n\n\xe2\x80\xa2 Tests\\Feature\\ExampleTest > 示例\n错误
\n调用未定义的方法 Tests\\Feature\\ExampleTest::visit()
\n
我目前正在跑步Laravel 8.6并且PHPUnit 9.510
任何关于如何解决这个问题的想法都受到高度欢迎。
\n我尝试运行 phpunit 并收到此错误:
PHP Fatal error: Declaration of Illuminate\Foundation\Testing\TestCase::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp(): void in /Users/xxx/dev/xxx/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php on line 65
Run Code Online (Sandbox Code Playgroud)
所以我找到了解决方案:在测试中添加:void和setUp()方法tearDown。
所以我改变了laravel里面的文件:vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php
public function setUp():void
{
parent::setUp();
}
public function tearDown():void
{
parent::tearDown();
}
Run Code Online (Sandbox Code Playgroud)
它有效,但重点是我正在修改vendor目录内的一个文件,并且它可能会被作曲家命令覆盖。
如何应用此解决方案而不修改vendor目录内的文件?
我的composer.json 文件
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=7.2",
"aloha/twilio": "^4.0",
"aws/aws-sdk-php": "^3.154",
"bensampo/laravel-enum": "^1.11",
"crockett/csv-seeder": "^1.1",
"cviebrock/eloquent-sluggable": "^4.5",
"fideloper/proxy": "^4.0",
"guzzlehttp/guzzle": …Run Code Online (Sandbox Code Playgroud) 我曾经问过这个代码有什么问题:
static $result = null;
if (isNull($result))
{
return $result;
}
Run Code Online (Sandbox Code Playgroud)
经过大量的downvotes(问题消失),问题是没有这样的东西叫做isNull函数.我们拥有的是is_null
大.我想知道我最初是如何使用isNull的.
原来这是自动完成工作.
现在我发现了为什么我输入isNull.该功能实际存在.我按ctrl单击,然后我去
/**
* Returns a PHPUnit_Framework_Constraint_IsNull matcher object.
*
* @return PHPUnit_Framework_Constraint_IsNull
* @since Method available since Release 3.3.0
*/
function isNull()
{
return PHPUnit_Framework_Assert::isNull();
}
/**
* Returns a PHPUnit_Framework_Constraint_IsTrue matcher object.
*
* @return PHPUnit_Framework_Constraint_IsTrue
* @since Method available since Release 3.3.0
*/
function isTrue()
{
return PHPUnit_Framework_Assert::isTrue();
}
Run Code Online (Sandbox Code Playgroud)
它来自一个名为functions.php的文件.我不知道那是什么问题
<?php
/**
* PHPUnit
*
* Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
* All …Run Code Online (Sandbox Code Playgroud)