vendredi 1 mars 2019

getting error message with phpunit testing in laravel 5

I got a class Fight, which implements a logic of a fight between two heroes. After the fight one of the hero may lose some health points. i wrote a php Unit test but getting an error

TypeError: Argument 1 passed to Tests\Unit\Fight::makeFight() must implement interface Tests\Unit\HeroInterface, string given, called in location\ExampleTest.php on line 61

here is my code with classes and interface and the test method

interface HeroInterface
{

    public function getForce(): int;

    public function getImmunity(): int;

    public function getHealthPoints(): int;

    public function setHealthPoints(int $healthPoints);
}

class DamageHelper
{
    const DAMAGE_RAND_FACTOR = 0.2;

    public static function getDamage(HeroInterface $attacker, HeroInterface $defender)
    {
        if ($attacker->getForce() < $defender->getForce()) {
            return 0;
        }

        $damageBase = round(($attacker->getForce() - $defender->getForce()) / $defender->getImmunity());

        $damageFactor = $damageBase * self::DAMAGE_RAND_FACTOR;
        $minDamage    = $damageBase - $damageFactor;
        $maxDamage    = $damageBase + $damageFactor;

        return mt_rand($minDamage, $maxDamage);
    }
}

class Fight
{

    public function makeFight(HeroInterface $attacker, HeroInterface $defender)
    {
        $damage = DamageHelper::getDamage($attacker, $defender);
        $defender->setHealthPoints($defender->getHealthPoints()-$damage);
    }
}

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testMakeFight()
    {
        $fight = new Fight();

        $this->assertTrue($fight->makeFight('10', '2'));
    }
}

any help will be appreciated



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire