Skip to content

Latest commit

 

History

History
29 lines (20 loc) · 830 Bytes

how-to-load-json-test.md

File metadata and controls

29 lines (20 loc) · 830 Bytes

How to load JSON in a unit test

Add json file to your test target.

Set it's target membership.

Then load

func testCanParseWeatherViaJSONFile() throws {

    guard let pathString = Bundle(for: type(of: self)).path(forResource: "weather", ofType: "json") else {
        fatalError("json not found")
    }

    guard let json = try? String(contentsOfFile: pathString, encoding: .utf8) else {
        fatalError("Unable to convert json to String")
    }

    let jsonData = json.data(using: .utf8)!
    let weatherData = try! JSONDecoder().decode(WeatherData.self, from: jsonData)
    
    XCTAssertEqual(10.58, weatherData.main.temp)
    XCTAssertEqual("Calgary", weatherData.name)
}

Links that help