Skip to content
Advertisement

Why my dataProvider function in my unit test file is not working properly?

I want to test this function:

JavaScript

Inside my unit test file, I have these functions:

JavaScript

And finally, I have these errors:

Expectation failed for method name is “get” when invoked zero or more times Parameter 0 for invocation IlluminateHttpRequest::get(‘address1′, null): mixed does not match expected value. Failed asserting that two strings are equal. Expected :’address2′ Actual :’address1’

Expectation failed for method name is “get” when invoked zero or more times Parameter 0 for invocation IlluminateHttpRequest::get(‘address1′, null): mixed does not match expected value. Failed asserting that two strings are equal. Expected :’address3′ Actual :’address1’

Etc…

I have the impression that my test function (testHandleShouldBeOk) only considers the first array of dataProvider.

I followed this documentation: https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html and, I also tried to set yield in dataProvider but, I still have the same errors.

Can you please tell me where I’m wrong?

Thank’s in advance.

EDIT: Thank @Alister Bulman, I stopped using a Request mock. I use a real Request object.

It’s work:

JavaScript

Advertisement

Answer

You’ve got 14 different tests being run – one for each part of the data-provider. You are testing each one against 14x fields, and 13 of them won’t be set. You can test that (setting all the fields) – or have your test work closer to how it would be expected – returning NULL for anything it doesn’t know.

Using a real Request, and not a mock may be helpful here, since it appears to return NULL for things it does not know.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement