PHPUnit @dataProvider 根本不起作用 [英] PHPUnit @dataProvider simply doesn't work

查看:22
本文介绍了PHPUnit @dataProvider 根本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读有关该主题的文档,并且我的代码遵循数据提供程序实现的所有要求.首先,这里是测试的完整代码,以防万一.

I've read the documentation on the topic, and my code follows all requirements of a data provider implementation. First of all, here's the full code of the test just in case it's relevant.

这是实现数据提供者的函数:

Here's the function implementing data provider:

/**
 * Test the createGroup function
 *
 * @return void
 * @author Tomas Sandven <tomas191191@gmail.com>
 *
 * @dataProvider provideFileImportTests_good
 **/
public function testCreateGroup($file, $groupname, $group, $mapping)
{
    // Create a test group
    $id = $this->odm->createGroup($groupname, $group);

    // Try to load it back out
    $result = R::load(OmniDataManager::TABLE_GROUP, $id);

    // Check that the result is not null
    $this->assertFalse(is_null($result));

    return $id;
}

PHPUnit 失败了:

PHPUnit just fails:

testsroadnetroadmapOmniDataManagerTest::testCreateGroup() 缺少参数 1

Missing argument 1 for testsroadnetroadmapOmniDataManagerTest::testCreateGroup()

我已尝试在数据提供程序函数中终止应用程序 (die();),但从未发生过.数据提供者函数在同一个类中公开可用,函数名称中没有拼写错误,testCreateGroup 函数在注释的注释中引用它,但从未调用数据提供者函数.

I've tried killing the application (die();) inside the data provider function, and it never happens. The data provider function is available publicly in the same class, there are no typos in the function name and the testCreateGroup function references it in the annotations in the comment, but the data provider function is never called.

有人知道为什么吗?

推荐答案

最后,经过几个小时的测试,我发现仅仅定义构造函数会破坏数据提供者的功能.很高兴知道.

Finally after hours of prodding this test file, I discovered that merely defining the constructor function breaks the functionality of data providers. Good to know.

要修复它,只需调用父构造函数.这是我的情况:

To fix it, just call the parent constructor. Here's how that looked in my case:

public function __construct()
{
    // Truncate the OmniDataManager tables
    R::wipe(OmniDataManager::TABLE_GROUP);
    R::wipe(OmniDataManager::TABLE_DATA);

    parent::__construct();   // <- Necessary
}


作为 David HarknessVasily 在评论中指出,构造函数覆盖必须匹配基类构造函数的调用签名.在我的例子中,基类构造函数不需要任何参数.我不确定这是否只是在较新版本的 phpunit 中发生了变化,或者它是否取决于您的用例.


As David Harkness and Vasily pointed out in the comments, the constructor override must match the call signature of the base class constructor. In my case the base class constructor didn't require any arguments. I'm not sure if this has just changed in newer versions of phpunit or if it depends on your use case.

无论如何,Vasily 的示例可能更适合您:

In any case, Vasily's example might work better for you:

public function __construct($name = null, array $data = array(), $dataName = '')
{
    // Your setup code here

    parent::__construct($name, $data, $dataName)
}

这篇关于PHPUnit @dataProvider 根本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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