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

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

问题描述

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

以下是实现数据提供程序的功能:

/**
 * 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失败:

tests \ broadnet \ broadmap \ OmniDataManagerTest :: testCreateGroup()缺少参数1

我尝试杀死数据提供程序功能内的应用程序(die();),但从未发生.数据提供程序功能可在同一类中公开使用,函数名称中没有拼写错误,并且testCreateGroup函数在注释的批注中引用了它,但从未调用数据提供程序功能.

有人知道为什么吗?

解决方案

最后,在测试该文件数小时之后,我发现仅定义构造函数会破坏数据提供程序的功能.很高兴知道.

要修复此问题,只需调用父构造函数即可.这是我的情况:

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

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

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 just fails:

Missing argument 1 for tests\broadnet\broadmap\OmniDataManagerTest::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.

Does anybody know why?

解决方案

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
}

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

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