Swift - Get NSBundle for test target?

南楼画角 提交于 2019-12-04 15:42:55

问题


In Objective C whenever I had to access the test bundle I would call

[NSBundle bundleForClass:[ClassInTestTarget class]];

bundleForClass is not available in Swift, so how do I access the test bundle?

The reason I need this is that in order to write tests for a framework I'm working on I have to use an in memory core data stack. Initially all resources were included in both main target and test target, but then I moved all these test helpers to test target only, and now the code below is returning nil which fails all my tests

let modelURL = NSBundle.mainBundle().URLForResource("Model", withExtension: "momd")

回答1:


The corresponding Swift code is

// Swift 3 and later:
Bundle(for: ClassInTestTarget.self)

// Swift 1, 2:
NSBundle(forClass: ClassInTestTarget.self)

ClassInTestTarget.self return the class object for the "ClassInTestTarget" class.



来源:https://stackoverflow.com/questions/25596702/swift-get-nsbundle-for-test-target

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!