2

I have a Post object that should be able to download it's image file from a server using Alamofire.

I'd really like to be able to test this with XCTest so I placed a file in the supporting files and attempt to download it with

let urlString: String = "\(bundleResourcePath)\(mediaURL)"
    let mediaPath : String = NSHomeDirectory() + "/Documents/postImages/\(id!).igo"
    var error : NSErrorPointer = NSErrorPointer()
    NSFileManager.defaultManager().createDirectoryAtPath(NSHomeDirectory()+"/Documents/postImages", withIntermediateDirectories: true, attributes: nil, error: error)
    let url : NSURL = NSURL(string: urlString)!
    NSFileManager.defaultManager().removeItemAtPath(mediaPath, error: nil)
    Alamofire.download(.GET, url) { (temporaryURL, response) in
        if let directoryURL = NSFileManager.defaultManager()
            .URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
            as? NSURL {
                let pathComponent = "postImages/\(self.id!).igo"
                return directoryURL.URLByAppendingPathComponent(pathComponent)
        }
        return temporaryURL
        }.response({ (request, response, data, error) -> Void in
            if error != nil {
                println(error!.localizedDescription)
            }
            self.image = UIImage(contentsOfFile: mediaPath)
            NSNotificationCenter.defaultCenter().postNotificationName(PostImageDownloadedNotification, object: self)
        })

However I get an error stating "unsupported URL". Fair enough thats because the string is a filepath. So I tried let url = NSURL(fileURLwithpath:) but what I get then is a runtime error:

Could not cast value of type 'NSURLResponse' () to 'NSHTTPURLResponse' ().

Does anyone know how I might be able to go about testing this with out having a server set up somewhere

AndyRyan
  • 1,500
  • 12
  • 23
  • I'm curious what you ended up doing, but my solution was to create a 3 line sinatra server to serve the image from, as presented [here](http://stackoverflow.com/a/19580397/1959155) – EthanP Nov 23 '15 at 22:19

0 Answers0