Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

murilo's avatar
Level 10

Use a class that has constructor method inside Pest php

hello , I have a class here of a service , but I cant use inside Pest php . becouse it does seams to assept a constructor class .

for example , I have this Payment Service


<?php
use App\Services\AuthService;

test('test connection', function ()
{
    $paymentService = new PaymentService();
    return $paymentService->processPayment();
});


Payment Service Class

class PaymentService
{

public function __construct(DefaultResponse $defaultResponse)
    {
        $this->defaultResponse = $defaultResponse;
}
// here I will process the ayment
public function processPayment () {

	}

// here I will process register new users
public function registerNewUsers () {

	}
}

it will give this error -

  Too few arguments to function App\Services\PaymentService::__construct(), 0 passed in /var/www/html/tests/Feature/Auth/LoginTest.php on line 11 and exactly 1 expected

0 likes
3 replies
LaryAI's avatar
Level 58

It looks like you're encountering an issue because your PaymentService class requires a DefaultResponse object as a parameter in its constructor, but you're not providing it when you instantiate the class in your Pest test.

To resolve this issue, you need to create an instance of DefaultResponse (or a mock of it if appropriate) and pass it to the PaymentService constructor in your test. Here's how you can modify your test to include this:

<?php
use App\Services\PaymentService;
use App\Services\DefaultResponse;

test('test connection', function () {
    $defaultResponse = new DefaultResponse(); // Assuming DefaultResponse can be instantiated like this.
    $paymentService = new PaymentService($defaultResponse);
    return $paymentService->processPayment();
});

If DefaultResponse has its own dependencies or requires complex setup, you might consider using a mocking library like Mockery to create a mock of the DefaultResponse class. Here's an example using Mockery:

<?php
use App\Services\PaymentService;
use App\Services\DefaultResponse;
use Mockery as m;

test('test connection', function () {
    $mockedResponse = m::mock(DefaultResponse::class);
    $paymentService = new PaymentService($mockedResponse);
    return $paymentService->processPayment();
});

This approach allows you to isolate the test to focus on the functionality of PaymentService without needing to fully implement DefaultResponse or its dependencies. Make sure to include any necessary setup or assertions based on what processPayment does and what you need to test about its behavior.

murilo's avatar
Level 10

cant I make like this ?

<?php
use App\Services\PaymentService;
use Mockery;

test('test connection', function ()
{
    $paymentService = Mockery::mock(PaymentService::class);
    dd($paymentService->processPayment());
});

Received Mockery_0_App_Services_PaymentService:::testConnection(), but no expectations were specified

the new DefaultResponse(), needs to go inside PaymentService

@laryai

martinbean's avatar

@murilo You can’t just instantiate a class like new PaymentService() if that class needs arguments in the constructor. The error message tells you as much:

Too few arguments

It requires 1 argument. You passed 0.

Please or to participate in this conversation.