Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Wanna unit-test a function, but constructor gives error

I am trying to unit test a function which is in an entity class, and it is stored in my DB by the use of a constructor. Each time I am trying to test this function it is giving me that error

ArgumentCountError: Too few arguments to function App\Entity\Deal::__construct(), 0 passed in /var/www/html/casus/tests/dealsEntityFunctionsTest.php on line 10 and exactly 1 expected

It is obvious I think, but I am really new with unit testing and that stuff so I couldn’t find the answer. Could you please help me?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My code is

class Deal
{
    private bool $isNewToday

    public function __construct($deal)
    {
        $this->isNewToday = $deal['is_new_today'];
    }

    public function getIsNewToday(): ?bool
    {
        return $this->isNewToday;
    }

    public function setIsNewToday(bool $isNewToday): self
    {
        $this->isNewToday = $isNewToday;

        return $this;
    }
}

And my unit test is

class test extends TestCase
{
    public function testIsNewTodayIsTrue()
    {
        $deal = new Deal();
        $deal->setIsForSale(true);
        $this->assertTrue($deal->getIsForSale(), true);
    }
}

>Solution :

As brombeer suggested, new Deal entity requires parameter.
This parameter looks like an array, with key ‘is_new_today’. So, sth like this below should help with constructor error.

class test extends TestCase
{
    public function testIsNewTodayIsTrue()
    {
        $deal = new Deal(['is_new_today' => true]);
        $deal->setIsForSale(true);
        $this->assertTrue($deal->getIsForSale(), true);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading