1

我在一些单元测试和覆盖方面遇到问题。

class InputFileReaderSpec: QuickSpec {

    override func spec() {

        beforeEach {

        }

        describe("InputFileReader") {
            it("should be able to read a file") {
                let inputFileReader = InputFileReader()
                let string = try? inputFileReader.readFileAt("f_digital-security-incident.yml")
                expect(string).notTo(beNil())
            }

            it("should try to read a file doesnt exist") {
                let inputFileReader = InputFileReader()
                expect {
                    try inputFileReader.readFileAt("test.yml")
                    }.to(throwError(InputFileReaderError.inputFileNotFound))

            }

            it("should try to read a file with format invalid") {
                let inputFileReader = InputFileReader()
                expect {
                    try inputFileReader.readFileAt("tool_cobian1.png")
                    }.to(throwError(InputFileReaderError.invalidFileFormat))
            }
        }

        afterEach {

        }
    }
}

我的 InputFileReader

import Foundation

enum InputFileReaderError: Error {
    case inputFileNotFound
    case invalidFileFormat
}

class InputFileReader {

         func readFileAt(_ fileName: String) throws -> String {
        guard let path = Bundle(for: type(of: self)).path(forResource: fileName, ofType: "") else {
            throw InputFileReaderError.inputFileNotFound
        }

        guard let content = try? String(contentsOfFile: path) else {
            throw InputFileReaderError.invalidFileFormat
        }
        return content
    }
}

发生的问题是覆盖率显示它没有对代码片段进行测试,但是在运行时它通过并经过测试。

覆盖范围

有没有人经历过这个? 覆盖范围

4

0 回答 0