Phpunit: خطأ في عد وسيطة موفر البيانات

تم إنشاؤها على ١٩ ديسمبر ٢٠١٦  ·  4تعليقات  ·  مصدر: sebastianbergmann/phpunit

أنا أستخدم أحدث إصدار من PHPUnit (5.7.4). المشكلة هي أنه عندما أجري الاختبار ، فإنه يمر. ولكن إذا قمت بتشغيله مرة أخرى ، فإنه يفشل مع الخطأ التالي:

ArgumentCountError: Too few arguments to function ValidationTest::testValidateType(), 0 passed and at least 3 expected

إذا قمت بإجراء أي تغييرات على وظيفة مزود البيانات (أي تغيير البيانات المراد إرجاعها أو اسم أو نوع وظيفة الموفر وما إلى ذلك) وإعادة تشغيلها ، فإنها تمر مرة واحدة وتفشل مع الخطأ أعلاه لجميع عمليات التشغيل الاختبارية المتتالية.

لست متأكدًا ، لكن هل تستخدم PHPUnit أي آلية تخزين مؤقت لتخزين بيانات الموفر مؤقتًا؟ إذا كانت الإجابة بنعم ، فهل هناك أي طريقة لتنظيفه (ربما باستخدام setUp أو tearDown


الكود التالي يعمل مرة واحدة (مرر مرة واحدة):

/**
 * <strong i="15">@covers</strong> Validation
 * <strong i="16">@coversDefaultClass</strong> Validation
 */
class ValidationTest extends TestCase {

    protected $validation;


    protected function setUp() {
        $this->validation = new Validation();
    }


    /**
     * <strong i="17">@covers</strong> ::validateType
     * <strong i="18">@dataProvider</strong> validateTypeProdiver
     */
    public function testValidateType($assertion, $argument, $type) {
        $result = $this->validation->validateType($argument, $type);

        switch ($assertion) {
            case 'True':
                $this->assertTrue($result);
                break;
        }
    }


    public function validateTypeProdiver() {
        return [
            ['True', 'file.txt', 'str']
       ];
    }
}

الكود التالي يعمل دائمًا (يمر في كل مرة):

/**
 * <strong i="24">@covers</strong> Validation
 * <strong i="25">@coversDefaultClass</strong> Validation
 */
class ValidationTest extends TestCase {

    protected $validation;


    protected function setUp() {
        $this->validation = new Validation();
    }


    /**
     * <strong i="26">@covers</strong> ::validateType
     */
    public function testValidateType() {
        foreach ($this->validateTypeProdiver() as $args) {
            $result = call_user_func_array([$this->validation, 'validateType'], array_slice($args, 1));

            switch ($args[0]) {
                case 'True':
                    $this->assertTrue($result);
                    break;
            }
        }
    }


    public function validateTypeProdiver() {
        return [
            ['True', 'file.txt', 'str']
       ];
    }
}

التعليق الأكثر فائدة

إذا واجه شخص ما هذه المشكلة.
كان لدي هذا الاختبار

class Test extends TestCase {
    protected $tested;

    public function __construct()
    {
        parent::__construct();
        $this->tested = new TestedClass();
    }

    public function provider(): array
    {
        return array(
            array(0, 1, 1),
            array(1, 2, 3),
        );
    }

    /**
     * <strong i="7">@dataProvider</strong> provider
     */
    public function testSum(int $first, int $second, int $expected)
    {
        $this->assertEquals($expected, $this->tested->sum($first + $second));
    }
}

لقد غيرت __construct إلى setUp وهو يعمل.

ال 4 كومينتر

لا يمكنني إعادة إظهار المشكلة بالمعلومات التي قدمتها. هذا ما فعلته:

$ composer require phpunit/phpunit:5.7.4
$ vendor/bin/phpunit && vendor/bin/phpunit

$ vendor/bin/phpunit && vendor/bin/phpunit 
PHPUnit 5.7.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.0.17-2+deb.sury.org~trusty+1
Configuration: /.../phpunit.xml

.                                                                   1 / 1 (100%)

Time: 44 ms, Memory: 4.00MB

OK (1 test, 1 assertion)
PHPUnit 5.7.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.0.17-2+deb.sury.org~trusty+1
Configuration: /.../phpunit.xml

.                                                                   1 / 1 (100%)

Time: 32 ms, Memory: 4.00MB

OK (1 test, 1 assertion)

هذه هي فئة الاختبار التي استخدمتها:

use PHPUnit\Framework\TestCase;

class TestMeTest extends TestCase
{

    protected $validation;

    protected function setUp()
    {

        $this->validation = new Validation();
    }

    /**
     * <strong i="9">@covers</strong> ::validateType
     * <strong i="10">@dataProvider</strong> validateTypeProdiver
     */
    public function testValidateType($assertion, $argument, $type)
    {

        $result = $this->validation->validateType($argument, $type);

        switch ($assertion) {
            case 'True':
                $this->assertTrue($result);
                break;
        }
    }

    public function validateTypeProdiver()
    {

        return [
            ['True', 'file.txt', 'str']
        ];
    }
}

والفئة Validation :

class Validation
{
    public function validateType() {
        return true;
    }
}

لا تقوم PHPUnit بالتخزين المؤقت لموفري البيانات بشكل دائم.

أنا أيضا لا أستطيع إعادة إنتاج هذا.

إذا واجه شخص ما هذه المشكلة.
كان لدي هذا الاختبار

class Test extends TestCase {
    protected $tested;

    public function __construct()
    {
        parent::__construct();
        $this->tested = new TestedClass();
    }

    public function provider(): array
    {
        return array(
            array(0, 1, 1),
            array(1, 2, 3),
        );
    }

    /**
     * <strong i="7">@dataProvider</strong> provider
     */
    public function testSum(int $first, int $second, int $expected)
    {
        $this->assertEquals($expected, $this->tested->sum($first + $second));
    }
}

لقد غيرت __construct إلى setUp وهو يعمل.

حل dmirogin جعل يومي ، phpunit 8.5

هل كانت هذه الصفحة مفيدة؟
0 / 5 - 0 التقييمات

القضايا ذات الصلة

kunjalpopat picture kunjalpopat  ·  4تعليقات

greg0ire picture greg0ire  ·  4تعليقات

sebastianbergmann picture sebastianbergmann  ·  3تعليقات

TiMESPLiNTER picture TiMESPLiNTER  ·  3تعليقات

rentalhost picture rentalhost  ·  4تعليقات