Birim testlerim için FluentAssertions kullanıyorum ve istisnaların doğru atılıp atılmadığını kontrol etmeye başladım. ExpectedExceptions
yöntem özniteliğini kullanabileceğimi biliyorum, ancak mümkün olduğunda FluentAssertion yaklaşımını öğrenmek istiyorum. FluentAssertions İstenmeyen bir operatör için istisna iddiası atıldı
Matrix
sınıf var:
public class Matrix
{
public int Rows { get; set; }
public int Columns { get; set; }
public float[,] Elements { get; set; }
public static Matrix operator *(Matrix m1, Matrix m2)
{
if (m1.Columns != m2.Rows)
{
throw new Exception("These matrices cant be multiplied");
}
return new Matrix(1, 2, new float[,] { {1, 2} });
}
}
ve ben İstisna durum için test etmek istiyorum. Bu defa ne var:
[TestMethod]
//[ExpectedException(typeof(Exception), "These matrices cant be multiplied")]
public void MatrixMultiplication_IncorrectMatrixSize_ExceptionTest()
{
// Arrange
var elementsA = new float[,]
{
{4, 7},
{6, 8}
};
var elementsB = new float[,]
{
{3, 0},
{1, 1},
{5, 2}
};
Matrix A = new Matrix() {Rows=2, Columns=2, Elements=elementsA);
Matrix B = new Matrix() {Rows=3, Columns=2, Elements=elementsB);
// Act
Func<Matrix, Matrix, Matrix> act = (mA, mB) => mA * mB;
// Assert
act(A,B).ShouldThrow<Exception>().WithInnerMessage("These matrices cant be multiplied");
}
I having FluentAssertions genel Func
için ShouldThrow
uzatma yöntemi yoktur ve ya bu sarmak için nasıl olmadığından emin değilim ki aksiyon. FluentAssertions bu şekilde bu şekilde kullanılabilir mi, yoksa FluentAssertions'ı farklı bir şekilde mi kullanmalıyım yoksa ExpectedExceptions
mu kullanmalıyım?
değil [docs] Do https://github.com/dennisdoomen/fluentassertions/wiki # istisnalar) bunu açıklıyor musunuz? – DavidG
(örneğin bir Eylem olarak: Action act = (mA, mB) => {var x = mA * mB;} ') –
DavidG
@DavidG Dokümanlara bakıyordum, ama benim için Bu özel durumda ne yapmam gerektiğini netleştiremiyorum. Haklısınız, bu şekilde bir 'Eylem 'yazabilirim ve' Func' yerine onu kullanabilirim, ancak 'ShouldThrow' uzantısı hala uygulanamaz. – Ayb4btu