dongliuxia9495 2019-01-29 15:42
浏览 49
已采纳

PHPUnit使用dataProvider返回“数据集#0无效。”

This simple class return

1) Warning The data provider specified for App\Tests\Twig\GenerateTokenTest::testGenerateToken is invalid. Data set #0 is invalid.

class GenerateTokenTest extends TestCase
{
    /**
     * @dataProvider provideToken
     */
    public function testGenerateToken(int $length): void
    {
        $token = GenerateToken::generate($length);

        $this->assertTrue(true);
    }

    public function provideToken(): iterable
    {
        yield 8;
        yield 16;
        yield 29;
    }
}

Do you know why ?

  • 写回答

1条回答 默认 最新

  • doulan9287 2019-01-29 15:57
    关注

    If you use data providers for PhpUnit, they expect that an array of data is returned on each call. This inner array should match the input variables for your test method. So, if it has that single argument $length, the data provider should return an array containing the test input, like yield [8];

    If the test method uses more than a single input variable - you've probably guessed it already: the array should contain more values. As an example, based on your code:

    class GenerateTokenTest extends TestCase
    {
        /**
         * @dataProvider provideToken
         */
        public function testGenerateToken(int $length, int $size): void
        {
            $token = GenerateToken::generate($length);
    
            $this->assertTrue(true);
        }
    
        public function provideToken(): iterable
        {
            yield [8, 9];
            yield [16, 17];
            yield [29, 30];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀