lukas653's avatar

How to test static Eloquent model

Hi, I have an issue with testing my classes and services. When my service classes have an Eloquent model inside. I try to use Mockery::mock('overload:'.Subscription::class), but unfortunately, I receive an error Mockery\Exception\RuntimeException : Could not load mock App\Subscription, class already exists even docblock on the top of the function does not help. Could you please help me to solve this issue :)

  /**
     * @runInSeparateProcess
     * @preserveGlobalState disabled
     */

Test

public function testService()
    {
        $service = new SubscriptionManagementService();

        $class = Mockery::mock('overload:' . Subscription::class);

        $service->sendSubscriptionsList('[email protected]');
    }

Service

    public function sendSubscriptionsList(string $email): void
    {
        $subscriptions = $this->getSubscriptions($email);

        if ($subscriptions->isEmpty()) {
            throw new SubscriptionNotFoundException();
        }

        event(new SendEmail($email, $subscriptions));
    }

    private function getSubscriptions(string $email): Collection
    {
        return Subscription::where('clients.email', $email)->get();
    }
0 likes
0 replies

Please or to participate in this conversation.