UICollectionView 单元测试 dequeueReusableCellWithReuseIdentifier 上的错误访问

UICollectionView Unit Test Bad Access on dequeueReusableCellWithReuseIdentifier

为了总结我的问题,我正在尝试测试集合视图的数据源。我初始化一个视图控制器并调用 viewDidLoad() 来初始化组件,这些组件初始化自定义 class 以用作数据源。这是我正在测试的class。

程序在此处开始单元测试:

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 模拟器上的应用程序时,此代码按预期运行。

如有任何帮助,我们将不胜感激。

这已通过在添加新数据进行测试后调用 reloadData() 解决。尝试模拟视图控制器来测试数据源时。 viewController.collectionView.reloadData() 必须调用。我从来没有考虑过这个,因为它是在 viewDidLoad() 调用中完成的,而且是在添加数据之前完成的。