2016-04-07 14 views
2

Ich habe FluentAssertions für meine Komponententests verwendet und habe angefangen, zu prüfen, ob Exceptions korrekt ausgelöst werden. Ich weiß, dass ich das Methodenattribut ExpectedExceptions verwenden kann, aber ich möchte den FluentAssertion-Ansatz lernen, wenn es möglich ist.FluentAssertions Das Aktivieren einer Ausnahme wurde für einen überladenen Operator ausgelöst.

Ich habe eine Matrix Klasse (für dieses Beispiel vereinfacht) mit einem überladenen Multiplikationsoperator:

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} }); 
    } 
} 

und ich würde für den Ausnahmefall testen möchten. Das ist, was ich habe, so weit:

[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"); 
} 

Das Problem, das ich habe, ist, dass FluentAssertions keine ShouldThrow Erweiterungsmethode für ein generisches Func haben, und ich bin nicht sicher, ob oder wie diese umwickeln eine Handlung. Ist es möglich, FluentAssertions auf diese Weise für diese Situation zu verwenden, oder verwende ich FluentAssertions auf andere Weise oder muss ich ExpectedExceptions verwenden?

+0

Sie nicht die [docs] (https://github.com/dennisdoomen/fluentassertions/wiki # Ausnahmen) erklären dies? – DavidG

+0

(zB als Aktion: 'Aktion act = (mA, mB) => {var x = mA * mB;}') – DavidG

+0

@DavidG Ich habe mir die Dokumente angesehen, aber für mich hat es nicht funktioniert mache mir nicht klar, was ich in diesem speziellen Fall tun soll. Du hast recht, ich kann auf diese Weise eine'Aktion 'schreiben und sie anstelle eines'Func' verwenden, aber die Erweiterung 'ShouldThrow' kann immer noch nicht angewendet werden. – Ayb4btu

Antwort

1

Hooray das Problem für Grübeln ...

die Testmethod wie dieses Schreiben machte es funktioniert:

[TestMethod] 
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 
    Action act =() => { var x = A * B; }; 

    // Assert 
    act.ShouldThrow<Exception>().WithMessage("These matrices cant be multiplied"); 
}