NSURL to file path in test bundle with XCTest

后端 未结 8 1576
栀梦
栀梦 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

    0 讨论(0)
  • 2020-12-23 19:30

    In fact, the [NSBundle mainBundle] when running a UnitTest is not the path of your app, but is /Developer/usr/bin, so this will not work.

    The way to get resources in a unit test is here: OCUnit & NSBundle

    In short, use:

    [[NSBundle bundleForClass:[self class]] resourcePath]
    

    or in your case:

    [[NSBundle bundleForClass:[self class]] resourceURL]
    
    0 讨论(0)
提交回复
热议问题