5

Is it allowed to return objects from @dataProviders to test methods?

public function iOsMessages()
{
    return [
        'update available'        => [1, new UpToDateMessage(), 'pl'],
    ];
}

/**
 * @test
 * @dataProvider iOsMessages
 */
public function success_create_message_for_ios(int $appVersion, MobileMessage $message, string $locale)
{
(...)

Error:

The data provider specified for Tests\Tests\Mobile\Classes\AppVersionTest::success_create_message_for_ios is invalid. Class 'Mobile\Classes\Messages\UpToDateMessage' not found

MobileMessage is an interface, imports are correct.

wujt
  • 1,278
  • 2
  • 13
  • 21
  • 2
    The error message says the class doesn't exist. PHPUnit is not guilty for that. Check the configuration of the autoloader and/or the class name and namespace. – axiac Feb 05 '18 at 15:24
  • Just remember that the data providers are executed only once before all the tests from a test case. – axiac Feb 05 '18 at 15:25
  • @axiac Does it make any difference? Those providers are used only once in separate tests. – wujt Feb 05 '18 at 15:33

1 Answers1

4

From the documentation:

A data provider method must be public and either return an array of arrays or an object that implements the Iterator interface and yields an array for each iteration step.

Abraham
  • 8,525
  • 5
  • 47
  • 53
Mat
  • 833
  • 1
  • 5
  • 20