Summer Sale! All accounts are 50% off this week.

normykinz's avatar

Pest and Exceptions

Here's an oddity.

I have a test that's about as simple as they get ...

it('throws an exception if a tenant cannot be found by hashed id', function () {
    get('/verify/xxxx/xxxx');
})->throws(TenancyException::class);

The controller method it's tesing goes like this ...

public function store(string $hashedId, string $token): void
    {
        $tenant = Tenant::findHash($hashedId);
        throw_unless($tenant, TenancyException::class, 'Token with hashed id not found');

        $tenantToken = $tenant->token(TokenType::ACTIVATION);
        throw_unless($tenantToken->token == $token, TenancyException::class, 'Token is invalid');

        $tenant->status = TenantStatus::LIVE;
        $tenant->save();
    }

I'm getting the rather odd result of the test ...

 Failed asserting that exception of type "App\Exceptions\TenancyException" is thrown.

  The following exception occurred during the last request:

  App\Exceptions\TenancyException: Token with hashed id not found in

Is anybody able to shed some light on why this test is failing? It doesn't make any sense to me!

0 likes
9 replies
Sinnbeck's avatar

You are doing a get request to a method named store()?

normykinz's avatar

@Sinnbeck Yeah, I'm not worried about restful actions for now. It was the test I was why the test was failing I'm interesred in.

normykinz's avatar

@Sinnbeck No, manually doing the check and thtowing the exception rather than using the throw_unless helper doesn't work either.

Sinnbeck's avatar

@normykinz any change if you disable middleware?

$this->withoutMiddleware();
get('/verify/xxxx/xxxx');
normykinz's avatar
normykinz
OP
Best Answer
Level 4

@Sinnbeck It seems that disabling framework exception handling with $this->withoutExceptionHandling() was the key. Thanks for the pointers @sinnbeck.

Sinnbeck's avatar

@normykinz happy to help. Please remember to set a best answer to set the thread as solved

Please or to participate in this conversation.