Ios UICollectionView单元测试对dequeueReusableCellWithReuseIdentifier的错误访问

Ios UICollectionView单元测试对dequeueReusableCellWithReuseIdentifier的错误访问,ios,swift,unit-testing,exc-bad-access,Ios,Swift,Unit Testing,Exc Bad Access,为了总结我的问题,我正在尝试测试集合视图的数据源。我初始化一个视图控制器,并调用viewDidLoad()来初始化初始化自定义类以用作数据源的组件。这是我正在测试的类 程序从单元测试开始,如下所示: class BubbleViewManagerTest: XCTestCase { var viewController : DashboardViewController = DashboardViewController() //var bubbleViewManager : BubbleVi

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

程序从单元测试开始,如下所示:

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

程序在以下行中断:
cell=collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifierMediumPriority,forIndexPath:indexPath)为!BubbleCollectionViewCell

我知道当一个对象在自动释放后被访问,或者在一开始没有初始化时,会发生EXC_BAD_访问。在我的例子中,我得到了错误,并确保所有对象都已初始化,并且它们的引用是强的(而不是弱的)。我已经启用了NSZombies,但运气不好

让我困惑的是
func collectionView(collectionView:cellForItemAtIndexPath:)->UICollectionViewCell
在线程中断之前已经执行过一次

这可能与我在测试目标上运行的事实有关吗?当我在iOS模拟器上运行应用程序时,此代码按预期运行


任何帮助都将不胜感激。

在添加新数据进行测试后,通过调用
reloadData()
解决了此问题。尝试模拟视图控制器以测试数据源时<必须调用代码>viewController.collectionView.reloadData()。我从未考虑过这一点,因为它是在
viewdiload()
调用中完成的,并且是在添加数据之前完成的。

同样的问题也适用于
UITableView
,使用相同的解决方案。
    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
}