2015-06-25 44 views
3

總結我的問題我試圖測試集合視圖的數據源。我初始化視圖控制器並調用viewDidLoad()來初始化初始化自定義類以用作數據源的組件。這是我正在測試的課程。UICollectionView單元測試對dequeueReusableCellWithReuseIdentifier訪問不良訪問

程序開始在單元測試在這裏:

class BubbleViewManagerTest: XCTestCase { 

var viewController : DashboardViewController = DashboardViewController() 
//var bubbleViewManager : BubbleViewManager = BubbleViewManager() 

override func setUp() { 
    super.setUp() 

    let storyboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType)) 
    viewController = storyboard.instantiateViewControllerWithIdentifier("DashboardViewControllerId") as! DashboardViewController 
    bubbleViewManager = viewController.bubbleViewManager 
    viewController.loadView() 
    viewController.viewDidLoad() 
} 

func testCellForItemAtIndexPath() { 
    DataManager.singleton.dayActivities = [] 

    DataManager.singleton.addGoal(Period(name: "asdf", hour: 1, minute: 1, color: UIColor.blackColor(), priority: PeriodPriority.Low)) 

    var cell = bubbleViewManager.collectionView(viewController.bubbleView, cellForItemAtIndexPath: NSIndexPath(forRow: 0, inSection: 0)) as! BubbleCollectionViewCell 

    XCTAssertEqual(cell.bounds.width, 45) 
    XCTAssertEqual(cell.bounds.height, 45) 
    XCTAssertNotNil(cell.bubbleView.period) 

    DataManager.singleton.addGoal(Period(name: "asdf", hour: 1, minute: 1, color: UIColor.blackColor(), priority: PeriodPriority.Medium)) 
    DataManager.singleton.addGoal(Period(name: "asdf", hour: 1, minute: 1, color: UIColor.blackColor(), priority: PeriodPriority.High)) 

    var index = NSIndexPath(forRow: 1, inSection: 0) 

    cell = bubbleViewManager.collectionView(viewController.bubbleView, cellForItemAtIndexPath: index) as! BubbleCollectionViewCell 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let period: Period = DataManager.singleton.dayActivities[indexPath.row] 
    var cell: BubbleCollectionViewCell = BubbleCollectionViewCell() 

    switch period.priority { 
     case .High: 
      cell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifierHighPriority, forIndexPath: indexPath) as! BubbleCollectionViewCell 
      break; 
     case .Medium: 
      cell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifierMediumPriority, forIndexPath: indexPath) as! BubbleCollectionViewCell 
      break; 
     case .Low: 
      cell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifierLowPriority, forIndexPath: indexPath) as! BubbleCollectionViewCell 
    } 

    // Give the bubble view the period for it to work on. 
    cell.bubbleView.period = period 


    // The bubble view needs to monitor a timer to redraw the bubble. Observer is assigned here. 
    if let timer = cell.bubbleView.period?.globalTimer { 
     timer.observer = cell.bubbleView 
    } 

    return cell 
} 

上線cell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifierMediumPriority, forIndexPath: indexPath) as! BubbleCollectionViewCell

我明白了一個EXC_BAD_ACCESS發生當一個對象被訪問的程序中斷它的自動釋放後或者當它沒有初始化的時候。在我的情況下,我得到的錯誤,並確保所有的對象初始化和他們的引用強(不弱)。我已經啓用NSZombies,並沒有運氣。

令我感到困惑的是func collectionView(collectionView:cellForItemAtIndexPath:) -> UICollectionViewCell已經在線程中斷之前執行過一次。

這可能與我在測試目標上運行的事實有關嗎?當我在iOS模擬器上運行應用程序時,此代碼按預期運行。

任何幫助將不勝感激。

回答

2

這是通過在添加新數據進行測試之後調用reloadData()來解決的。嘗試模擬視圖控制器以測試數據源時。必須調用viewController.collectionView.reloadData()。我從來沒有考慮過這個問題,因爲它是在viewDidLoad()調用中完成的,而且這是在添加數據之前完成的。