NSHomeDirectory in iPhone unit test

When using NSHomeDirectory() inside unit test, I get:

 /Users/hgpc/Library/Application Support/iPhone Simulator/5.0 

I was expecting the actual home directory of the current application. Sort of:

 /Users/hgpc/Library/Application Support/iPhone Simulator/5.0/Applications/6D1091C6-02AE-4803-A7DF-D623D0F89579 

What am I doing wrong?

+6
source share
4 answers

To solve this problem, set the Bundle Loader value in the settings of the target Unit Test assembly to:

$(BUILT_PRODUCTS_DIR)/MyAppName.app/MyAppName

as well as your test node:

$(BUNDLE_LOADER)

you must find NSHomeDirectory() to return the correct value.

+5
source

Unfortunately, while in a unit test (or a logical test) - you are not really "in your application" (ie its sandbox). This is why things like NSDocumentDirectory or [NSBundle mainBundle] will not work.

If this works for you, I’m just going to create the Documents folder in

 /Users/hgpc/Library/Application Support/iPhone Simulator/5.0 

You might want to do this in your test suite, so you can remove it in tearDown.

If this does not work because your tests depend on what is already in your NSDocumentDirectory application, you may need to think a little about your test, since they should all be autonomous (i.e. set all the resources from your package to setUp )

You can also use NSLibraryDirectory instead of NSDocumentDirectory, depending on what you want to check.

+3
source

If you want to configure the test object to test the application, you must add the parameters mentioned by Andrew Ebling:

 $(BUILT_PRODUCTS_DIR)/MyAppName.app/MyAppName 

as well as your test node:

 $(BUNDLE_LOADER) 

If you do not have these parameters, it means that your tests are logical. But if you add these parameters, you will change the tests for application tests and notice that AppDelegate will start to run tests.

see link for more information.

This way you can use NSHomeDirectory (application only).

+1
source

On an iOS device you should use

 NSArray *path = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDirPath = [paths objectAtIndex:0]; 
0
source

Source: https://habr.com/ru/post/903058/


All Articles