Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
283 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...