本文介绍了Laravel PHPunit Dataprovider 错误“指定的数据提供者无效"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 dataprovider 并且数据是从数据库中获取的:

I am using dataprovider and data is fetched from database:

public function logoProvider()
{

    $result = Event::inRandomOrder()->select('id')->whereNotNull('logo')->first();

    return [
        [$result->id, '<path>'],
        ['', ''],
    ];
}

但出现以下错误:

The data provider specified for Tests\Unit\****Test::testLogo is invalid.
Error: Call to a member function connection() on null

有什么解决办法吗?

推荐答案

感谢他们的提问.以我的拙见,恕我直言,真的是他们!,错误消息发布:

Thank they for their question. In my humble opinion and with all due respect Truly Theys!, the error message posted:

错误:在 null 时调用成员函数 connection()

在我看来可能是由以下调用链中隐藏的数据库访问层引起的:

appears to me is likely caused by a hidden database-access-layer in the following call chain:

Event::inRandomOrder()->select('id')->whereNotNull('logo')->first()

在我看来,当调用数据提供程序时,好像隐藏的数据库访问层之类的东西没有工作(或未准备好).

And it looks to me as-if something like a hidden database-access-layer is not working (or is not ready) when the data-provider is called.

有什么解决办法吗?

根据我对数据提供者摘录上下文中的错误消息的印象,我认为一个潜在的解决方案可能是在调用链之前设置(配置)数据库访问层.

With my impression of the error message in context of the data-provider excerpt, I'd assume that a potential solution could be to set up (configure) the database-access-layer before the call-chain.

在我看来,配置是整个测试装置的一部分.

It appears to me that the configuration is part of the overall test fixture.

当看到此错误消息并假设配置是夹具的一部分时,解决方案是在执行数据提供程序时设置夹具.

When seeing this error message and given the configuration is part of the fixture, the solution is to set-up the fixture when the data-provider is executed.

根据数据提供者可以[原文]从setUp获取连接 标准的 setUpsetUpBeforeClass 钩子不适用于数据提供者方法.

According to Dataprovider can [sic] get connection from setUp standard setUp and setUpBeforeClass hooks aren't applicable for data-provider methods.

因此,另一个解决方案可能是使用 bootstrap-file Phpunit 设置.引导是在运行任何测试用例之前完成的,绑定到全局静态的数据提供者会发现它已配置.

Therefore an additional solution may be using the bootstrap-file Phpunit setting. Bootstrapping is done before running any test-case and data-providers that are bound to the global static state will find it configured.

我会考虑什么作为替代解决方案?

What would I consider as an alternative solution?

根据我自己的经验 - 它们的里程可能会有所不同 - 在我看来,在 Phpunit 中使用数据提供程序时,通常最直接并且通常足以决定首先在没有数据库连接的情况下进行测试.

In my own experience - their mileage may vary - it appears often to me most straight forward and often sufficient to decide to test without a database connection first of all when using a data-provider in Phpunit.

当有一个更大的测试数据集时,数据提供者可以很容易地从作为测试套件一部分的夹具文件中获得,如果运行测试套件和文件系统通常至少可用于读取访问测试套件.

When having a larger data-set for a test, the data-provider can easily get if from fixture files which are part of the test-suite and a file-system is normally available at least for read-access when running the test-suite.

然后任何数据提供者只返回简单的测试参数,而不使用其他昂贵的数据层和设置.

Any data-provider then just returns the plain test-arguments without making use of other expensive data layers and setups.

最近版本的 Phpunit 也打乱了所提供数据的顺序,就像测试运行程序对测试方法执行顺序所做的那样.

Recent versions of Phpunit also shuffle the order of the data provided, as the test-runner does for the order of how test methods are executed.

另请参阅此用 phpunit 模拟 PDO"的回答.

这并不意味着没有需要数据库适配器和网络服务测试数据库的特定测试(通常称为集成测试数据库测试em>).

That must not mean that there ain't specific tests that require a database adapter and a over-the-network serviced test-database (often called integration tests or database tests).

需要使用数据库适配器运行的测试然后在它自己的测试套件中完成,该套件可以更专用于测试与数据库适配器的集成以及与数据库系统的连接.

Tests requiring running with a database adapter are then done in a test-suite of it's own that can be run more dedicated to test the integration with the database adapter and as well with the connection to a database system.

之所以这样做是因为使用数据库连接的测试通常要贵得多 - 以多种方式:配置/设置,使用它编写/维护测试和数据,最后但并非最不重要的是运行测试 - 以便他们很容易成为快速反馈循环中的阻碍,因此他们只会专门针对不同的特定开发目标(例如每晚)执行这些反馈.

The division is done because tests with a database connection are often much more expensive - in multiple ways: configuration/set-up, writing/maintaining the test and data with it and last but not least running the test - so that they can easily become a blocker in the fast feedback loop so they only execute them specifically dedicated towards a differently specific development goal (for example nightly).

这篇关于Laravel PHPunit Dataprovider 错误“指定的数据提供者无效"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 09:32
查看更多