Assert
Assert.Equal, Assert.True ve Assert.Throws ile iddiaları öğrenirsin.
Assert sınıfı
Assert, beklenen ile gerçek durumu karşılaştıran statik bir sınıftır. Sık kullanılan metotlar: Assert.Equal(expected, actual) eşitliği, Assert.True(condition) doğru olması gerektiğini, Assert.Throws<T>(action) belirli bir istisna fırlatılmasını denetler.
Her Assert başarısız olursa bir istisna fırlatır; bu istisna testin başarısız olduğunu bildirir. Doğrulama ne kadar netse, hata iletisi de o kadar anlamlı olur. Bu yüzden Assert.Equal(5, result) tercih edilir; Assert.True(result == 5) geriye dönük olarak daha az bilgi verir.
Uygulama MathHelper için üçlü doğrulama
FactAttribute ve statik Assert sınıfını tanımla. Assert.Equal<T>, Assert.True(bool) ve Assert.Throws<T>(Action) metotları olsun; başarısızlıkta Exception fırlatsınlar. MathHelper.IsEven(int) çift sayıda true, tekte false, sıfır altında ArgumentException fırlatsın. MathHelperTests sınıfında üç [Fact] metodu yaz: TestFourIsEven Assert.Equal(true, MathHelper.IsEven(4)); TestThreeIsNotEven Assert.True(!MathHelper.IsEven(3)); TestNegativeThrows Assert.Throws<ArgumentException>(() => MathHelper.IsEven(-1)) çağrısı yapsın. Main üçünü çalıştırıp "Test passed: IsEven checks" yazsın.
Örnek çözümü göster
static class Assert { public static void Equal<T>(T e, T a) { if (!EqualityComparer<T>.Default.Equals(e, a)) throw new Exception(); } public static void True(bool c) { if (!c) throw new Exception(); } public static void Throws<T>(Action a) where T : Exception { try { a(); } catch (T) { return; } throw new Exception(); } } tanımla. class MathHelper { public static bool IsEven(int n) { if (n < 0) throw new ArgumentException(); return n % 2 == 0; } } yaz. class MathHelperTests { [Fact] public void TestFourIsEven() { Assert.Equal(true, MathHelper.IsEven(4)); } [Fact] public void TestThreeIsNotEven() { Assert.True(!MathHelper.IsEven(3)); } [Fact] public void TestNegativeThrows() { Assert.Throws<ArgumentException>(() => MathHelper.IsEven(-1)); } } yaz. Main'de üç metodu çağırıp Console.WriteLine("Test passed: IsEven checks");
Örnek çözüm:
using System;
class FactAttribute : Attribute { }
static class Assert
{
public static void Equal<T>(T expected, T actual)
{
if (!EqualityComparer<T>.Default.Equals(expected, actual))
throw new Exception($"Beklenen {expected}, gerçek {actual}");
}
public static void True(bool condition)
{
if (!condition) throw new Exception("Beklenen: doğru");
}
public static void Throws<T>(Action action) where T : Exception
{
try { action(); }
catch (T) { return; }
throw new Exception("Beklenen istisna fırlatılmadı");
}
}
class MathHelper
{
public static bool IsEven(int n)
{
if (n < 0) throw new ArgumentException();
return n % 2 == 0;
}
}
class MathHelperTests
{
[Fact]
public void TestFourIsEven()
{
Assert.Equal(true, MathHelper.IsEven(4));
}
[Fact]
public void TestThreeIsNotEven()
{
Assert.True(!MathHelper.IsEven(3));
}
[Fact]
public void TestNegativeThrows()
{
Assert.Throws<ArgumentException>(() => MathHelper.IsEven(-1));
}
}
class Program
{
static void Main()
{
var tests = new MathHelperTests();
tests.TestFourIsEven();
tests.TestThreeIsNotEven();
tests.TestNegativeThrows();
Console.WriteLine("Test passed: IsEven checks");
}
}
Assert.Throws
Assert.Throws<T>(action), bir eylemin belirli bir istisna fırlatmasını bekler. Hata yolunun da test edilmesi gerekir; aksi halde yalnızca mutlu yol denetlenmiş olur. Lambda ile kısa yazılır: Assert.Throws<ArgumentException>(() => Parse("")).
Eğer action istisna fırlatmazsa veya farklı türden istisna fırlatırsa test başarısız olur. Bu sayede sözleşmenin ("boş giriş reddedilir") uygulamada korunduğu doğrulanır.
Kendini test et
-
Doğrulama çağrısını tamamla
Assert.____(5, Calculator.Add(2, 3));Cevabı göster
Assert.Equal(5, Calculator.Add(2, 3)); -
Hangi Assert bir koşulun doğru olduğunu denetler?
- Assert.Equal
- Assert.Same
- Assert.True
- Assert.Throws
Cevabı göster
Assert.True — Doğru. Assert.True(condition) koşulun doğru olduğunu bekler.
-
Assert metotlarını eşleştir
- Assert.Equal
- İki değerin eşitliği
- Assert.True
- Koşulun doğru olması
- Assert.Throws
- Belirli istisna fırlatılması
- Assert.Null
- Değerin null olması