NSURL to file path in test bundle with XCTest

后端 未结 8 1627
栀梦
栀梦 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:23

    1. You can reference bundle files like in any target.
    2. Check if the file is Copyed in the Copy Bundle Resources build phase (of your test target)
    3. To access to the local file :

      NSURL*imageUrl=[[NSBundle mainBundle]URLForResource:@"imageName" withExtension:@"png"];
      

    You can make asynchronous access and wait for the response using : https://github.com/travisjeffery/TRVSMonitor

    If you have added : dataset1.json in the test target (2) :

    NSString *p=[[NSBundle mainBundle] pathForResource:@"dataset1" ofType:@"json"];
    NSLog(@"%@",p);
    

    2013-10-29 15:49:30.547 PlayerSample[13771:70b] WT(0): /Users/bpds/Library/Application Support/iPhone Simulator/7.0/Applications/7F78780B-684A-40E0-AA35-A3B5D8AA9DBD/PlayerSample.app.app/dataset1.json

提交回复
热议问题