jeudi 25 février 2016

Unit Testing days in a given month & year failing when should be correct. Swift 2

I have the below function within my AddNewViewController class:

func dayCount(year : Int, month : Int) -> Int {
    switch (month) {
    case 2:
        return year % 4 == 0 && year % 100 != 0 ? 29 : 28
    case 1, 3, 5, 7, 8, 10, 12:
        return 31
    case 4, 6, 9, 11:
        return 30
    default:
        return 0 // Invalid month number
    }
}

within my test class i have the following code:

import UIKit
import XCTest

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

override func tearDown() {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    super.tearDown()
}

 func testDesignIsDisplayedAfterViewLoads() {
        let vc = AddNewViewController()
        let days = vc.dayCount(2016, month: 2)
        XCTAssertEqual(29, days, "The days in Feb 2016 should be 29 as its a leap year")
    }

}

When I try and run the test it says test failed and I'm confused as to why.

Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire