2014-06-05 5 views
17

Esiste qualche equivalente per verificare l'esistenza di eccezioni di lancio nella lingua swift Test unitari?Test unità Swift con XCTAssertThrows analogico

Per esempio ho una classe:

class Square : NSObject{ 

    let sideLength: Int 

    init(sideLength: Int) { 
     assert(sideLength >= 0, "Wrong initialization of Square class with below zero side length") 
     self.sideLength = sideLength 
     super.init() 
    } 
} 

e Test per verificare il lavoro. Nell'obiettivo C posso scrivere un metodo di prova come questo:

- (void)testInitializationWithWrongSideLengthThrowsExceptions{ 
    XCTAssertThrows([[Shape alloc] initWithSideLength: -50], "Should throw exceptions on wrong side values initialisations"); 
} 

Che cos'è Swift uguale tecnica?

+3

@Vignesh Kumar: Per favore basta smettere del tutto retagging domande Swift. Non stai aiutando. Affatto. – BoltClock

risposta

2

Se si aggiungono i seguenti tre file per i test:

// ThrowsToBool.h 
#import <Foundation/Foundation.h> 

/// A 'pure' closure; has no arguments, returns nothing. 
typedef void (^VoidBlock)(void); 

/// Returns: true if the block throws an `NSException`, otherwise false 
BOOL throwsToBool(VoidBlock block); 


// ThrowsToBool.m 
#import "ThrowsToBool.h" 

BOOL throwsToBool(VoidBlock const block) { 
    @try { 
     block(); 
    } 
    @catch (NSException * const notUsed) { 
     return YES; 
    } 
    return NO; 
} 


// xxxTests-Bridging-Header.h 
#import "ThrowsToBool.h" 

Poi si può scrivere:

XCTAssert(throwsToBool { 
    // test code that throws an NSException 
}) 

Ma non funziona per far valere o precondizione :(

PS ho avuto l'idea da: http://modocache.io/xctest-the-good-parts

2

Penso che la funzione assert() debba essere utilizzata solo a scopo di debug. Non solo per la seguente dichiarazione di Apple Swift-Book (https://itun.es/de/jEUH0.l):

“Le asserzioni causano la vostra applicazione per terminare e non sono un sostituto per la progettazione il codice in modo tale che le condizioni non valide sono improbabile che ciò avvenga.”

Ecco perché vorrei risolvere questo come segue:

import Cocoa 
import XCTest 

class Square 
{ 
    let sideLength: Int 

    init(_ sideLength: Int) 
    { 
     self.sideLength = sideLength >= 0 ? sideLength : 0 
    } 
} 

class SquareTests: XCTestCase 
{ 
    override func setUp() { super.setUp() } 
    override func tearDown() { super.tearDown() } 

    func testMySquareSideLength() { 
     let square1 = Square(1); 
     XCTAssert(square1.sideLength == 1, "Sidelength should be 1") 

     let square2 = Square(-1); 
     XCTAssert(square2.sideLength >= 0, "Sidelength should be not negative") 
    } 
} 

let tester = SquareTests() 
tester.testMySquareSideLength() 
+0

ovviamente dovrebbe. –

0

non esiste un equivalente di XCTAssertThrows a swift. Per ora non è possibile utilizzare una funzione nativa, ma esiste una soluzione con una guida all'obiettivo-c. Puoi usare Quick o solo Nimble. O per fare la propria funzione assert - vedi questo articolo - http://modocache.io/xctest-the-good-parts - potenziale di miglioramento # 2: Aggiungi XCTAssertThrows a Swift

0

modo corretto a Swift 2:

class Square : NSObject{ 

    let sideLength: Int 

    init(sideLength: Int) throws { // throwable initializer 
     guard sideLength >= 0 else { // use guard statement 
      throw ...// your custom error type 
     } 

     self.sideLength = sideLength 
     super.init() 
    } 
} 

e test:

func testInitThrows() { 
     do { 
      _ = try Square(sideLength: -1) // or without parameter name depending on Swift version 
      XCTFail() 
     } catch ... { // your custom error type 

     } catch { 
      XCTFail() 
     } 
    }