我有一个结果枚举和错误,如下所示:
enum Result<T>: Equatable {
case Success(T)
case Error(ErrorType)
}
func ==<T>(lhs: Result<T>, rhs: Result<T>) -> Bool {
var equal: Bool = false
switch (lhs, rhs) {
case (.Success, .Success):
equal = true
case (.Error, .Error):
equal = true
default:
break
}
return equal
}
RequestError的样子如下:
enum RequestError: String,
ErrorType,
Equatable {
case NoInternet = "NO_INTERNET_ERROR"
case Unknown = "UNKNOWN_ERROR"
case ServerError = "SERVER_ERROR"
}
init?(_ error: NSError?) {
//do init
}
func ==(lhs: RequestError, rhs: RequestError) -> Bool {
return lhs.rawValue == rhs.rawValue
}
我正在使用Quick+Nimble编写规范文档, 内容如下:
class ResultSpec: QuickSpec {
override func spec() {
describe("Result") {
context("when comparing 2 success results") {
it("returns true") {
let equal = Result.Success(5) == Result.Success(5)
expect(equal).to(beTrue())
}
}
context("when comparing 2 error results") {
it("returns true") {
let error = NSError(domain: "", code: 0, userInfo: nil)
let requestError = RequestError(error)!
let equal = Result.Error(requestError) == Result.Error(requestError)
expect(equal).to(beTrue())
}
}
}
}
}
第一个测试检查成功通过。第二个测试由于以下错误无法编译:
二元运算符“==”不能应用于两个“Result<_>”操作数
出错的位置是这一行:
let equal = Result.Error(requestError) == Result.Error(requestError)