NSURL to file path in test bundle with XCTest

后端 未结 8 1613
栀梦
栀梦 2020-12-23 18:35

I am trying to write an iOS app using TDD and the new XCTest framework. One of my methods retrieves a file from the internet (given a NSURL object) and stores it in the user

8条回答
  •  醉话见心
    2020-12-23 19:14

    Here's the Swift version of this, Xcode 7, iOS 9, etc.

    let testBundle = NSBundle(forClass: self.dynamicType)
    let path = testBundle.pathForResource("someImage", ofType: "jpg")
    XCTAssertNotNil(path)
    

    Note: someImage.jpg must be included in your test target.


    Edit: Swift 5

    let testBundle = Bundle(for: type(of: self))
    let path = testBundle.path(forResource: "someImage", ofType: "jpg")
    XCTAssertNotNil(path)
    

提交回复
热议问题