Проблем със задача 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
Thank you so much, but unfortunatelly none of your advice helped me
and Judge still gives me 93/100.
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);
}
}
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 :)