0

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.

enter image description here

john doe
  • 9,220
  • 23
  • 91
  • 167

1 Answers1

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
  • 1
    I thought if I am running the test project then bundle.main will be the test project. – john doe Apr 09 '20 at 12:27
  • 1
    I 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