spatie / pest-expectations

A collection of handy custom Pest customisations
https://spatie.be/open-source
MIT License
68 stars 3 forks source link

Translator is not instantiable #9

Closed bjarn closed 1 year ago

bjarn commented 1 year ago

Since the introduction of PR #8, failed assertions are throwing the following exception:

Illuminate\Contracts\Container\BindingResolutionException : Target [Illuminate\Contracts\Translation\Translator] is not instantiable.
 /Users/xxx/dev/code/laravel/xxx-backend/vendor/laravel/framework/src/Illuminate/Container/Container.php:1128
 /Users/xxx/dev/code/laravel/xxx-backend/vendor/laravel/framework/src/Illuminate/Container/Container.php:923
 /Users/xxx/dev/code/laravel/xxx-backend/vendor/laravel/framework/src/Illuminate/Container/Container.php:795
 /Users/xxx/dev/code/laravel/xxx-backend/vendor/laravel/framework/src/Illuminate/Container/Container.php:731
 /Users/xxx/dev/code/laravel/xxx-backend/vendor/spatie/pest-expectations/src/PestExpectations.php:50
 /Users/xxx/dev/code/laravel/xxx-backend/app/Domain/XXX/ValidationRules/XXXRule.php:27
 /Users/xxx/dev/code/laravel/xxx-backend/vendor/spatie/pest-expectations/src/PestExpectations.php:64
 /Users/xxx/dev/code/laravel/xxx-backend/tests/Unit/Rules/XXXRuleTest.php:38

It is reproducable with the following rule:

namespace App\Domain\XXX\ValidationRules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class XXXRule implements ValidationRule
{
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        $fail('test');
    }
}
it('XXXRule', function () {
    $rule = new XXXRule();

    expect($rule)
        ->toFailWith('test');
});

I am not using the translation function - just a string.

Downgrading seems to be a workaround - but not a solution of course. I tried to debug it, but can't seem to figure it out atm, might give it a shot again later.

freekmurze commented 1 year ago

@sdkakcy could you take a look at this one?

sdkakcy commented 1 year ago

@freekmurze okey, I will look.

sdkakcy commented 1 year ago

@bjarn can you tell your Laravel version?

bjarn commented 1 year ago

@sdkakcy Of course, running on Laravel v10.9.0 and Pest v1.23.0 (not v2 yet).

sdkakcy commented 1 year ago

Hi again @bjarn, sorry for late. I'm so busy nowadays. I have created a PR #10 for this issue. It fixes your problem. I have tested it on my side. But you must add your Folder/File in your Test Case section in Pest.php file. In my case: I store my rule tests in Unit folder. I added the Unit in here:

Screen Shot 2023-05-14 at 16 06 19 PM

bjarn commented 1 year ago

@sdkakcy no worries at all! Same here :)

I'll test it and let you know tomorrow!

sdkakcy commented 1 year ago

@bjarn the PR is not accepted yet fyi.

sdkakcy commented 1 year ago

@freekmurze could you look at the PR?

bjarn commented 1 year ago

@sdkakcy Just checked, fixed indeed after adding it to Pest.php! Thanks :)