Swift 给定月份的单元测试天数&;年份失败的时候应该是正确的。斯威夫特2
我的AddNewViewController类中有以下函数:Swift 给定月份的单元测试天数&;年份失败的时候应该是正确的。斯威夫特2,swift,unit-testing,testing,xctest,Swift,Unit Testing,Testing,Xctest,我的AddNewViewController类中有以下函数: 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, 1
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
}
}
在我的测试类中,我有以下代码:
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")
}
}
当我试着运行测试时,它说测试失败了,我不知道为什么
任何帮助都将不胜感激 一定还有别的事。测试没有失败,嗨,马丁。我没有任何其他测试。我已经将你的函数和测试复制到一个新创建的iOS项目中,并且没有失败。顺便说一句,如果一年可以被400整除,那么它就是闰年(例如2000年)。您的函数不包括这种情况。您应该使用NSCalendar进行此类计算。