Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

softy_02 avatar softy_02 20 Точки

Проблем със задача 04.Fighting Arena - C# OOP - Unit Testing - Exerice

Здравейте,

Задачата е 04.Fighting Arena от упражнението по Unit Testing. Judge ми дава 93/100 и грешка на 8-ми тест. Ще се радвам ако някой погледне кода и ми помогне да разбера какво пропускам. Благодаря предварително :)

 

Линк към Judge -> https://judge.softuni.bg/Contests/Compete/Index/1762#3

Arena Tests -> https://pastebin.com/H1fiQeDe

Warrior Tests -> https://pastebin.com/Cg01Q0du 

0
C# OOP Advanced
Axiomatik avatar Axiomatik 2422 Точки

Hi,

Your warrior-tests seem OK, so I think that the problem comes from your arena-tests:

1.There is no unit-test for the arena-constructor (meaning if this.arena = new List<Warrior>(); is executed by the constructor). I think that this might be the unit-test that is missing to get 100% from judge.

[Test]

public void TestConstructorWorksCorrectly()

{

Assert.IsNotNull(this.arena.Warriors);

}

 

2.There is an alternative implementation for your CheckIfEnrollWorksProperly() unit-test:

[Test]

public void EnrollShouldAddWarriorToTheArena()

{

Warrior warrior1 = new Warrior("Warrior", 5, 50);

this.arena.Enroll(warrior1);

Assert.That(this.arena.Warriors, Has.Member(warrior1));

}

 

3.There is a further unit-test for the Enroll-method, where a hero with the same name BUT different memory reference should throw an exception, when another warrior with the same name is already found in the collection:

[Test]

public void EnrollingWarriorsWithSameNameShouldThrowException()

{

Warrior warrior1 = new Warrior("Warrior", 5, 50);

Warrior warrior1Copy = new Warrior(warrior1.Name, warrior1.Damage, warrior1.HP);

this.arena.Enroll(warrior1);

Assert.Throws<InvalidOperationException>(() =>

{

this.arena.Enroll(warrior1Copy);

});

}

 

4.Your unit-test CheckIfAttackWorksProperly() should assert the HP values of the attacker and defender after the fight-method has been called:

[Test]

public void TestFightMethodWithAttackerAndDefender()

{

this.arena.Enroll(new Warrior("Warrior1", 5, 50););

this.arena.Enroll(new Warrior("Warrior2", 5, 60););

 

int expectedAttackerHP = this.attacker.HP - this.defender.Damage;

int expectedDefenderHP = this.defender.HP - this.attacker.Damage;

 

this.arena.Fight(this.attacker.Name, this.defender.Name);

 

Assert.AreEqual(expectedAttackerHP, this.attacker.HP);

Assert.AreEqual(expectedDefenderHP, this.defender.HP);

}

0
softy_02 avatar softy_02 20 Точки

Thank you so much, but unfortunatelly none of your advice helped me

and Judge still gives me 93/100.

0
Axiomatik avatar Axiomatik 2422 Точки

OK, I used the default solution-setup without [TestCase] in one method, but with individual [Test] with separate methods. I don't see any problems with your Warrior-tests, so I guess that using TestCases was the problem.

When using Warrior-tests without TestCase, then your solution gives 100% :

 

[TestFixture]
    public class WarriorTests
    {
        [SetUp]
        public void Setup()
        {
        }

        [Test]
        public void TestConstructorIfWorksCorrectly()
        {
            string expectedName = "Kesho";
            int expectedDamage = 50;
            int expectedHP = 100;

            Warrior warrior = new Warrior(expectedName, expectedDamage, expectedHP);

            string actualName = warrior.Name;
            int actualDamage = warrior.Damage;
            int actualHP = warrior.HP;

            Assert.AreEqual(expectedName, actualName);
            Assert.AreEqual(expectedDamage, actualDamage);
            Assert.AreEqual(expectedHP, actualHP);
        }
        
        [Test]
        public void TestNameValidatorWithNullName()
        {
            string name = null;
            int damage = 50;
            int HP = 100;

            Assert.Throws<ArgumentException>(() =>
            {
                Warrior warrior = new Warrior(name, damage, HP);
            });
        }

        [Test]
        public void TestNameValidatorWithEmptyName()
        {
            string name = string.Empty;
            int damage = 50;
            int HP = 100;

            Assert.Throws<ArgumentException>(() =>
            {
                Warrior warrior = new Warrior(name, damage, HP);
            });
        }

        [Test]
        public void TestNameValidatorWithWhitespaceName()
        {
            string name = "         ";
            int damage = 50;
            int HP = 100;

            Assert.Throws<ArgumentException>(() =>
            {
                Warrior warrior = new Warrior(name, damage, HP);
            });
        }

        [Test]
        public void TestDamageValidatorWithZeroDamage()
        {
            string name = "Kesho";
            int damage = 0;
            int HP = 100;

            Assert.Throws<ArgumentException>(() =>
            {
                Warrior warrior = new Warrior(name, damage, HP);
            });
        }

        [Test]
        public void TestDamageValidatorWithNegativeDamage()
        {
            string name = "Kesho";
            int damage = -1;
            int HP = 100;

            Assert.Throws<ArgumentException>(() =>
            {
                Warrior warrior = new Warrior(name, damage, HP);
            });
        }

        [Test]
        public void TestHPValidatorWithNegativeHp()
        {
            string name = "Kesho";
            int damage = 50;
            int HP = -100;

            Assert.Throws<ArgumentException>(() =>
            {
                Warrior warrior = new Warrior(name, damage, HP);
            });
        }

        [Test]
        [TestCase(25)]
        [TestCase(30)]
        public void TestAttackerAttacksWithHPLowerThanOrEqual30ShouldThrowException(int attackerHP)
        {
            string attackerName = "Kesho";
            int attackerDamage = 10;

            string defenderName = "Gosho";
            int defenderDamage = 10;
            int defenderHP = 40;

            Warrior attacker = new Warrior(attackerName, attackerDamage, attackerHP);
            Warrior defender = new Warrior(defenderName, defenderDamage, defenderHP);

            Assert.Throws<InvalidOperationException>(() =>
            {
                attacker.Attack(defender);
            });
        }

        [Test]
        [TestCase(25)]
        [TestCase(30)]
        public void TestDefenderDefendsWithHPLowerThanOrEqual30ShouldThrowException(int defenderHP)
        {
            string attackerName = "Kesho";
            int attackerDamage = 10;
            int attackerHP = 50;

            string defenderName = "Gosho";
            int defenderDamage = 10;

            Warrior attacker = new Warrior(attackerName, attackerDamage, attackerHP);
            Warrior defender = new Warrior(defenderName, defenderDamage, defenderHP);

            Assert.Throws<InvalidOperationException>(() =>
            {
                attacker.Attack(defender);
            });
        }

        [Test]
        public void TestWeakerAttackerAttacksStrongerDefenderShouldThrowException()
        {
            string attackerName = "Kesho";
            int attackerDamage = 10;
            int attackerHP = 40;

            string defenderName = "Gosho";
            int defenderDamage = 80;
            int defenderHP = 40;

            Warrior attacker = new Warrior(attackerName, attackerDamage, attackerHP);
            Warrior defender = new Warrior(defenderName, defenderDamage, defenderHP);

            Assert.Throws<InvalidOperationException>(() =>
            {
                attacker.Attack(defender);
            });
        }

        [Test]
        public void TestAttackerDefenderHPShouldBeDecreasedBySuccessfullAttack()
        {
            string attackerName = "Kesho";
            int attackerDamage = 10;
            int attackerHP = 50;

            string defenderName = "Gosho";
            int defenderDamage = 10;
            int defenderHP = 50;

            Warrior attacker = new Warrior(attackerName, attackerDamage, attackerHP);
            Warrior defender = new Warrior(defenderName, defenderDamage, defenderHP);

            int expectedAttackerHP = attackerHP - defenderDamage;
            int expectedDefenderHP = defenderHP - attackerDamage;

            attacker.Attack(defender);

            Assert.AreEqual(expectedAttackerHP, attacker.HP);
            Assert.AreEqual(expectedDefenderHP, defender.HP);
        }

        [Test]
        public void TestStrongerAttackerShouldKillWeakerDefender()
        {
            string attackerName = "Kesho";
            int attackerDamage = 60;
            int attackerHP = 50;

            string defenderName = "Gosho";
            int defenderDamage = 10;
            int defenderHP = 50;

            Warrior attacker = new Warrior(attackerName, attackerDamage, attackerHP);
            Warrior defender = new Warrior(defenderName, defenderDamage, defenderHP);

            int expectedDefenderHP = 0;
            int expectedAttackerHP = attacker.HP - defender.Damage;

            attacker.Attack(defender);

            Assert.AreEqual(expectedAttackerHP, attacker.HP);
            Assert.AreEqual(expectedDefenderHP, defender.HP);
        }
    }

0
softy_02 avatar softy_02 20 Точки

Well, it turns out that the problem was actually in the test cases.

Thank you so much for all of your advice and help. Stay safe :)

0
dZf1aeA-GHDSPORTS avatar dZf1aeA-GHDSPORTS 2 Точки

 Unit tests give developers and testers a quick way to look for logic errors in the methods of classes in C#, Visual Basic, and ... Microsoft unit test framework for managed code GHDSports TV Apk

-2
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.