无法使用参数列表((ErrorType),XMPPError)调用'XCTAssertEqual'

时间:2016-10-11 07:42:37

标签: ios swift xcode rx-swift

在MyModule中,我有这个枚举:

enum MyError: ErrorType {
    case failToSendMessage
    case notAuthenticated
    case noResponseReceived
}

在MyModuleTests中:

import XCTest
@testable import MyModule

class MyModuleTests: XCTestCase {
    func testNotAuthenticated() {
        myClass.doSomething()
        .subscribeError { error in 
            XCTAssertEqual(error, MyError.notAuthenticated)
        }
    }
}

doSomething会返回Observable

为什么会收到以下错误消息: Cannot invoke 'XCTAssertEqual' with an argument list ((ErrorType), MyError)

3 个答案:

答案 0 :(得分:8)

您应该为MyError添加符合Equatable的扩展名。

extension MyError: Equatable
{
    static func == (lhs: MyError, rhs: MyError) -> Bool {
        switch (lhs, rhs) {
        case (.failToSendMessage, .failToSendMessage):
            return true;

        case (.notAuthenticated, .notAuthenticated):
            return true;

        case (.noResponseReceived, .noResponseReceived):
            return true;

        default:
            return false;
        }
    }
}

然后,断言你的错误非常简单。

if let error = result.error {
                    XCTAssertTrue(error == MyError.notAuthenticated,"API returns 403");
                } else {
                    XCTFail("Response was not an error");
                }

答案 1 :(得分:1)

如错误所示,您在尝试使用(ErrorType)

MyErrorXCTAssertEqual进行比较

1.检查错误原因(ErrorType)而不是ErrorType

2.在转换(类型转换)ErrorTypeMyError错误后尝试比较。

答案 2 :(得分:0)

事实证明,无法直接比较ErrorType:

XCTAssertEqual(error, MyError.notAuthenticated as ErrorType)
// Cannot invoke 'XCTAssertEqual' with an argument list ((ErrorType), ErrorType)

XCTAssert(error == (MyError.notAuthenticated as ErrorType))
// Binary operator '==' cannot be applied to two 'ErrorType' operands

为了完成同样的事情,我做到了这一点:

        switch error {
        case MyError.notAuthenticated:
            ()
        default:
            XCTFail()
        }