【问题标题】:PHPUnit test method won't receive array as a paremeterPHPUnit 测试方法不会接收数组作为参数
【发布时间】:2021-10-19 18:04:40
【问题描述】:

我有一个 PHPUnit 测试,其中第三种方法不会接收第二种方法中返回的数组。看看:

<?php

namespace Tests\Unit;

use Tests\TestCase;

class TesteTest extends TestCase
{
    public function testFirst()
    {
        $test = ['id' => 123];
        $this->assertNotEmpty($test);
        return $test;
    }

    /** @depends testFirst */
    public function testSecond(array $test)
    {
        $this->assertNotEmpty($test);
    }

    /** @depends testSecond */
    public function testThird(array $test)
    {
        $this->assertNotEmpty($test);
    }
    
}

这是我从 PHPUnit 得到的回复:

传递给 Tests\Unit\TesteTest::testThird() 的参数 1 必须是数组类型,给定 null,在 C:\Users\edgar\Documents\Projetos\Solarium\solarium-api\vendor\phpunit\ 中调用phpunit\src\Framework\TestCase.php 在第 1527 行

对我可能出错的地方有什么想法吗?

【问题讨论】:

    标签: unit-testing phpunit tdd laravel-8


    【解决方案1】:

    想法很简单。 Check documentation.

    • 生产者是一种测试方法,它产生其被测单元作为返回值。
    • 消费者是一种依赖于一个或多个生产者及其返回值的测试方法。

    您的生产者testSecond 不会向您的消费者testThird 任何回报

    public function testSecond(array $test)
    {
        $this->assertNotEmpty($test);
        return $test;
    }
    

    【讨论】:

    • 我打算返回方法接收到的数组。
    • @EdgardBertelli 比返回该数组 :) 现在你从 testSecond 没有返回任何内容
    猜你喜欢
    • 2018-08-05
    • 2014-07-06
    • 1970-01-01
    • 2021-12-04
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 2013-04-05
    相关资源
    最近更新 更多