首页 > 解决方案 > 如何读取 JSON.text 文件以快速生成数据类型?

问题描述

我正在尝试模拟网络结果数据并在 JSON.txt 中模拟它,我想执行 json 解析的单元测试,但似乎我总是无法读取 JSON.txt 文件

将要测试的方法:

class Music {


    func parseJSON(data : Data) -> [[String:Any]]? {

        return try? JSONSerialization.jsonObject(with: data, options: []) as! [[String:Any]]
    }
}

这是测试用例

import XCTest
@testable import basicUnitTesting

class musicTests: XCTestCase {


    func testParseJSON() {
        let music = Music()
        let bundle = Bundle(for: type(of: self))
        if let path = bundle.path(forResource: "JSON", ofType: "txt") {
            if let data = try? Data.init(contentsOf: URL.init(fileURLWithPath: path)) {
                let result = music.parseJSON(data: data)

                XCTAssertNotNil(result, "It should not be nil")
                XCTAssertGreaterThan(result!.count, 0, "It should have value, not zero")
            } else {
                XCTFail()
            }

        } else {
            XCTFail()
        }

    }



}

这是错误的屏幕截图:

在此处输入图像描述

如您所见,路径为零,然后测试失败。

这是 JSON.txt 中的数据,用于模拟数据

[{"id":"1","music_url":" http://orangevalleycaa.org/api/media/music/ModernHardRock_45450.wav ","name":"现代硬摇滚","description":"强大带有酷炫吉他即兴演奏、充满活力的常规鼓、紧凑的贝司和吉他独奏的摇滚曲目。非常适合用于运动(如拳击)视频、广告等。","duration":"2:18","image":" http: //orangevalleycaa.org/api/media/images/ArtistWorking_059730538.png ","thumb":" http://orangevalleycaa.org/api/media/images/thumbs/ArtistWorking_059730538.png ","created_by":"ikoliks" ,"file_name_original":"现代硬摇滚_LYNDA_45450.wav"},{"id":"2","music_url":" http://orangevalleycaa.org/api/media/music/AcousticIntro_45687.wav","name":"Acoustic Intro","description":"简短的原声吉他前奏,具有梦幻、轻松的品质,激发反射。","duration":"0:57","image":" http:// /orangevalleycaa.org/api/media/images/BalletInSmoke_054599628.png ","thumb":" http://orangevalleycaa.org/api/media/images/thumbs/BalletInSmoke_054599628.png ","created_by":"Trevor Lackey" ,"file_name_original":"Acoustic Intro_LYNDA_45687.wav"},{"id":"3","music_url":" http://orangevalleycaa.org/api/media/music/OptimisticLife_41224.wav ","name": 《乐观生活》、《说明》:”带有乐观氛围的歌曲,对于需要令人振奋和凉爽氛围的音乐视频来说,这是完美的背景。","duration":"0:30","image":"http://orangevalleycaa.org/api/media/images/BlowingGlass_066874921.png ","thumb":" http://orangevalleycaa.org/api/media/images/thumbs/BlowingGlass_066874921.png ","created_by":" PremiumAudio","file_name_original":"Optimistic Life_LYNDA_41224.wav"},{"id":"4","music_url":" http://orangevalleycaa.org/api/media/music/TraceyLarvenz_35879.wav ","name ":"Light and Oxygen","description":"适合企业、营销和励志视频的活泼、舒缓的曲目。","duration":"3:05","image":" http://orangevalleycaa .org/api/media/images/FireDancers_042981315.png ","thumb":"http://orangevalleycaa.org/api/media/images/thumbs/FireDancers_042981315.png","created_by":"Tracey Larvenz","file_name_original":"Tracey Larvenz track_LYNDA_35879.wav"},{"id":"5","music_url":" http://orangevalleycaa.org/api/media/ music/ExploreCaliforniaIntro_8773.wav ","name":"Exploring California","description":"一首合成器和打击乐曲目,就像沿着加利福尼亚海岸行驶一样顺畅。","duration":"0:20","image ":" http://orangevalleycaa.org/api/media/images/GlassBlownDragon_056473995.png ","thumb":" http://orangevalleycaa.org/api/media/images/thumbs/GlassBlownDragon_056473995.png ","created_by ":"布莱斯普尔","file_name_original":"探索加州 Intro_LYNDA_8773.wav"},{"id":"6","music_url":" http://orangevalleycaa.org/api/media/music/Aspire_45442.wav描述":"积极而鼓舞人心。梦想、想法和对新事物和充满美感的感觉。","duration":"2:06","image":"http://orangevalleycaa.org/api/media/images/GraphicArtist_052214820.png ","thumb":" http://orangevalleycaa.org/api/media/images/GraphicArtist_052214820.png ","created_by":"Julio Kladniew ","file_name_original":"Beginning In The Farm_LYNDA_41245.mp3"},{"id":"8","music_url":" http://orangevalleycaa.org/api/media/music/BrightAcousticAmbient_35443.wav "," name":"Bright Acoustic Ambient","description":"以 12 弦木吉他为主要乐器的原声吉他和尤克里里的醇厚音轨。","duration":"3:21","image":" http ://orangevalleycaa.org/api/media/images/PleinAirArtist_061873487。png ","拇指":" http://orangevalleycaa.org/api/media/images/thumbs/PleinAirArtist_061873487.png","created_by":"Eliosmusic","file_name_original":"明亮的音响环境_LYNDA_35443.wav"},{"id":"9","music_url":" http://orangevalleycaa.org/api/media/music /BrothersInArms_46815.wav ","name":"Brothers In Arms","description":"一部描绘战时英雄、胜利和阵亡的主题叙事作品。适合悲痛、情绪化的损失和悲剧场景。","duration":"1:15","image":" http://orangevalleycaa.org/api/media/images/SculptorWelding_053783433.png ","thumb" :" http://orangevalleycaa.org/api/media/images/thumbs/SculptorWelding_053783433.png ","created_by":"http://orangevalleycaa.org/api/media/music/BusinessCorporateBackgrounds_41443.wav ","name":"Business Corporate Backgrounds","description":"积极、振奋和鼓舞人心的柔和企业氛围音乐,非常适合作为背景音乐用于励志故事、广告、电视广告、品牌公司和新闻、科学和发明、建筑和贸易、技术和教育等等。","duration":"2:57","image":" http:// /orangevalleycaa.org/api/media/images/SpinningPottery_061544433.png ","thumb":" http://orangevalleycaa.org/api/media/images/thumbs/SpinningPottery_061544433.png ","created_by":"AZOVMUSIC", “文件名原件”:"企业背景_LYNDA_41443.wav"}]

标签: iosjsonswiftunit-testingtesting

解决方案


从错误中可以清楚地看出您的文件未包含在资源路径错误行中>if let path = bundle.path(forResource: "JSON", ofType: "txt")

所以,问题是你的文件不包括在这里musicTests > Build Phases > Copy Bundle Resources

在此处输入图像描述


推荐阅读