Laravel PhpUnit DataProvider错误'指定的数据提供程序无效' [英] Laravel PHPunit Dataprovider error 'The data provider specified is invalid'

查看:15
本文介绍了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 TestsUnit****Test::testLogo is invalid.
Error: Call to a member function connection() on null

有什么办法吗?

推荐答案

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

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

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

Error: Call to a member function connection() on null

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

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.

根据 Dataprovider 可以 [sic] 从 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".

See also the May 2020 edit in this answer to "Mocking PDO with phpunit".

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

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错误'指定的数据提供程序无效'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆