2014-06-05 6 views
17

Czy istnieje jakiś ekwiwalent sprawdzania rzucania wyjątków w szybkich testach jednostkowych?Testy Swift Unit z analogiem XCTAssertThrows

Na przykład mam klasy:

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

i test sprawdzający działać. W obiektywnym C mogę napisać taką metodę badania:

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

Co to jest Swift equic technic?

+3

@Vignesh Kumar: Proszę po prostu zatrzymać ponowne dodawanie tagów Swift pytania w ogóle. Nie pomagasz. W ogóle. – BoltClock

Odpowiedz

2

Jeśli dodać następujące trzy pliki do testów:

// 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" 

Następnie można napisać:

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

Ale to nie działa w przypadku stwierdzenia lub warunek :(

PS wpadłem na pomysł z: http://modocache.io/xctest-the-good-parts

2

Myślę, że funkcja assert() powinna być używana tylko do celów debugowania. Nie tylko z powodu następującego oświadczenia Apple Swift-Book (https://itun.es/de/jEUH0.l): „Asercje powodować swoją aplikację do rozwiązania i nie są substytutem dla projektowania kodu w taki sposób, że nieprawidłowe warunki są mało prawdopodobne, aby powstać”

to dlatego chciałbym rozwiązać ten problem w następujący sposób:

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

oczywiście powinno. –

0

nie ma odpowiednika do XCTAssertThrows w SWIFT. Na razie nie możesz używać natywnej funkcji, ale istnieje rozwiązanie z pewną pomocą celu. Możesz użyć Szybkiego lub tylko Zwinny. Lub zrobić własną funkcję wymuszenia - zobacz ten artykuł - http://modocache.io/xctest-the-good-parts - Potencjał Poprawa # 2: Dodaj XCTAssertThrows do Swift

0

poprawny sposób 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() 
    } 
} 

i testowanie:

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

     } catch { 
      XCTFail() 
     } 
    }