I am not sure why but I am not able to read a json file in my Swift Unit Testing Project. What am I missing. the url is empty object and it is just jumping to the completion(nil) line 20.
Asked
Active
Viewed 1,009 times
0
-
Did you add it to the Build Phases of your test target? – koen Apr 09 '20 at 02:46
-
why not just make the https call to open weather map.... its easier.. – jbiser361 Apr 09 '20 at 05:04
-
@jbiser361 True but let's say that OpenWeatherMap charges X amount whenever I make a call. – john doe Apr 09 '20 at 12:25
-
oh your right... they do – jbiser361 Apr 09 '20 at 21:55
1 Answers
3
Your code is using Bundle.main
, but you put the file in your test target(/bundle in the code context).
I'm not sure how to get the test bundle, but according to this question, you should be able to do this:
guard let url = Bundle(for: MockWeatherService.self).url(forResource: "openweathermap-response", withExtension: "json"),
and then the rest of your guard
statement

Sam
- 2,350
- 1
- 11
- 22
-
1I thought if I am running the test project then bundle.main will be the test project. – john doe Apr 09 '20 at 12:27
-
1I think bundles are dependent on your folder structure and the targets your files are defined for, so it doesn't change. But (assuming this solution works) it will work even if `Bundle.main` was the test bundle when running tests, because it gets the bundle of that specific class. – Sam Apr 09 '20 at 15:12