标签: phpunit

Php,如何模拟对象实例化?

所以,一个函数:

function testMe ($a, $b)
{
    $obj = new CalculationObject();
    return $obj->calcIt ($a, $b);
}
Run Code Online (Sandbox Code Playgroud)

如何嘲笑这个创作?

php phpunit mocking

1
推荐指数
1
解决办法
824
查看次数

Laravel 在 PHPUnit 测试期间断言多个电子邮件收件人

我想测试在 PHPUnit 测试期间电子邮件是否已发送到多个地址。我怎样才能做到这一点?

php email phpunit laravel

1
推荐指数
1
解决办法
427
查看次数

我如何测试 Laravel 护照 API 端点

所以我想对我的 API 端点进行单元测试。我正在使用 Laravel 5.8 并且使用 Passport 完成 api 身份验证我有以下测试:

public function guest_can_login()
{
    $user = factory(User::class)->create();

    $response = $this->json('POST', 'api/login', [
        'email' => $user->email,
        'password' => 'secret',
    ]);

    $response
        ->assertStatus(200)
        ->assertJson([
            'success' => true,
        ]);
}
Run Code Online (Sandbox Code Playgroud)

当我使用 Postman 执行请求时,它运行良好,对用户进行身份验证并返回一个令牌,但是当我启动测试时它失败了(“预期状态代码 200,但收到 500。”)

所以我在回复中更深入地搜索,我发现了这个:

"message": "未找到个人访问客户端。请创建一个。"

不知道为什么会发生这种情况,如果有人对此有所了解

编辑:固定见下面我的最后一个答案

phpunit laravel laravel-passport

1
推荐指数
1
解决办法
1789
查看次数

Laravel 5.2 - phpunit - 无法修改标头信息 - 标头已发送

你能帮我在我本地的宅基地/流浪机器上解决这个问题吗?

当我运行命令时:PHPUnit

我得到这个结果

PHPUnit 4.8.36 由 Sebastian Bergmann 和贡献者编写。

E
时间:1.97 秒,内存:6.00MB

                                                                                                               There was 1 error:                                                    

                                                                                                               1) ExampleTest::testBasicExample                                      
Run Code Online (Sandbox Code Playgroud)

无法修改标头信息 - 标头已发送(输出开始于 /home/vagrant/Code/Project/vendor/php unit/phpunit/src/Util/Printer.php:134)

                                                                                                               /home/vagrant/Code/Project/bootstrap/app.php:4                         
Run Code Online (Sandbox Code Playgroud)

/home/vagrant/Code/Poptin/tests/TestCase.php:20
/home/vagrant/Code/Project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:85 /home/vagrant/Code /Project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:64

请帮忙!我什么都试过了!!!

测试用例文件

<?php

class TestCase extends Illuminate\Foundation\Testing\TestCase
{
    /**
     * The base URL to use while testing the application.
     *
     * @var string
     */
    //protected $baseUrl = 'http://localhost';
    protected $baseUrl = 'http://192.168.10.10';

    /**
     * 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 …
Run Code Online (Sandbox Code Playgroud)

php phpunit laravel laravel-5 homestead

1
推荐指数
2
解决办法
3147
查看次数

Laravel - 如何在 phpunit 测试中设置伪造语言环境?

我可以将faker应用程序中的语言环境更改config/app.phppt_BR更改,'faker_locale' => 'pt_BR',并且它在我的工厂中运行良好,但在我的测试用例中运行良好。这就是我在我的测试中导入 faker 的方式:

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Models\Proprietario;

class ProprietarioTest extends TestCase
{
    use WithFaker, RefreshDatabase;


    public function testStore(){


        $attributes = [
            'name' => $this->faker->name,
            'email' => $this->faker->email,
            'address' => $this->faker->city,
            'phone' => $this->faker->cellPhoneNumber,
            'municipio' => $this->faker->randomDigit,
        ];
        $response = $this->post('/api/v1/proprietario', $attributes);

        $response->assertStatus(201);
        $createdArea = Proprietario::latest();
        $this->assertDatabaseHas('proprietarios', $attributes);
    }
Run Code Online (Sandbox Code Playgroud)

测试将失败,$this->faker->cellPhoneNumber因为它在默认语言环境中不可用。我正在使用 Laravel 5.8 和 PHP 7.2

phpunit faker laravel

1
推荐指数
1
解决办法
1316
查看次数

PHPunit 警告错误 testsuits not expected

我是 PHPunit 的新手。我用 composer 安装了 PHPunit 并创建了一个 phpunit.xml 文件:

    <?xml version="1.0" encoding="UTF-8" ?>

    <phpunit bootstrap="vendor/autoload.php" colors="true">
        <testsuits>
            <testsuit name="unit">
                <directory>tests</directory>
            </testsuit>
        </testsuits>
    </phpunit>
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息

PHPUnit 7.5.16 由 Sebastian Bergmann 和贡献者编写。

警告 - 配置文件未通过验证!检测到以下问题:

第 4 行: - 元素“testsuits”:不需要此元素。

测试结果可能与预期不符。

php phpunit

1
推荐指数
1
解决办法
2408
查看次数

Laravel 5.8 Illuminate\Validation\ValidationException:给定的数据无效

我的控制器中有这样的 store 方法

public function store()
{
     request()->validate(['family'=>'required']);
     User::create(request()->all());
     return redirect('/users');
}
Run Code Online (Sandbox Code Playgroud)

和这样的测试方法

/** @test */
public function a_user_require_family()
{
    $this->withoutExceptionHandling();
    $this->post('/users', [])->assertSessionHasErrors('family');
}
Run Code Online (Sandbox Code Playgroud)

它告诉我这个:

1) Tests\Feature\UserTest::a_user_require_family
Illuminate\Validation\ValidationException:给定的数据无效。

php phpunit laravel laravel-5 laravel-unit-test

1
推荐指数
1
解决办法
1185
查看次数

Laravel 6/7 测试:尚未设置外观根

我正在尝试为我的 Laravel 7 应用程序编写一些测试。但我一直面临A facade root has not been set.错误。

<?php

namespace Tests\Feature;

use Illuminate\Support\Facades\Config;
use PHPUnit\Framework\TestCase;

class AddressListenerRepositoryTest extends TestCase
{
    public function testCreate()
    {
        Config::set('x', 'address_listeners'); // Cause of "A facade root has not been set" error
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释这个错误的原因吗?

phpunit laravel laravel-testing laravel-7

1
推荐指数
1
解决办法
1215
查看次数

为什么 PHPUnit 9 不期望记录 phpunit.xml.dist 的 log 子元素

我目前正在将旧的 PHP 应用程序(超过 10 年)转换到 PHP 7.4,我们目前正在应用程序的 PHP 7.4 分支上实现持续集成。我们决定使用 PHP 当前稳定版本支持的 PHPUnit 版本。所以我们将 PHP 7.4 分支的 ci 测试工作升级到了 PHPUnit 9.3。

我们根据文档进行了所有必要的更改,但是我们因一个我们不知道如何修复的警告而被阻止(授予测试执行并且报告发布在正确的位置)警告 - 配置文件未通过验证!

  The following problems have been detected:

  Line 38:
  - Element 'log': This element is not expected..
Run Code Online (Sandbox Code Playgroud)

我在下面分享我们的 PHPUnit 配置,并使用我们用来启动它的命令,任何人都可以发现它有什么问题吗?

phpunit -c phpunit.xml.dist


<?xml version="1.0" encoding="UTF-8"?>
<phpunit
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/9.3/phpunit.xsd"
         colors                      = "true"
         bootstrap                   = "tests/bootstrap.php"
         >

    <testsuites>
        <testsuite name="Unit Tests">
            <directory>tests/</directory>
        </testsuite>
    </testsuites>
    <coverage>
        <include>
            <directory suffix=".php">api</directory>
        </include>
    </coverage>
    <logging>
        <log type="junit" target="build/unit_report.xml"/>
    </logging>
</phpunit>
Run Code Online (Sandbox Code Playgroud)

php phpunit

1
推荐指数
1
解决办法
1012
查看次数

当我在 Laravel 框架上运行测试时,PHPUnit 不起作用,我收到错误 /usr/bin/php 声明 PHP_VERSION 的值无效

我正在尝试使用命令 ./vendor/bin/phpunit --filter=testNoImportFile 运行我的测试文件 ImportCSVTest.php

但我得到了错误

/usr/bin/php declares an invalid value for PHP_VERSION.
This breaks fundamental functionality such as version_compare().
Please use a different PHP interpreter.
Run Code Online (Sandbox Code Playgroud)

我的测试文件ImportCSVTest.php如下

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Tests\TestCase;

class ImportCSVTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testNoImportFile()
    {
        Storage::fake('products');

        $response = $this->json('POST', '/import', [
            'csv' => UploadedFile::fake()->create('emptyUpload', 0, null)
        ]);

        Storage::disk('products')->assertMissing('emptyUpload');
    }


}
Run Code Online (Sandbox Code Playgroud)

我第一次遇到这样的错误,如何解决这个问题?

php testing macos phpunit

1
推荐指数
1
解决办法
886
查看次数