我遇到了一些单元测试和覆盖问题。
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
}
}
发生的问题是,覆盖范围显示它没有对代码段进行测试,但在运行时会通过并进行测试。
有没有人经历过这个? Coverage