1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Injecting a repository class in a service class and then trying to initiate service class in...

Discussão em 'Outras Linguagens' iniciado por Chris, Outubro 3, 2024 às 19:42.

  1. Chris

    Chris Guest

    Say I have:

    class SomeService implements SomeInterface
    {
    public function __construct(private SomeRepository $repo)
    {}

    public function createSomething(array $data)
    {
    }
    }


    If I want to test this service class with PHPUnit I will get an error "too few arguments passed to SomeService::__construct()"

    class SomeServiceTest {
    protected function setUp()
    {
    $this->service = new SomeService;
    }

    // some tests using above service...
    }


    I see lots of examples of this all over the internet - but why when it causes this error?

    Am I missing something? You don't want to have to pass the repo to the constructor in the test again.

    Am I approaching this the wrong way?

    Continue reading...

Compartilhe esta Página