Cannot call "XCTAssertEqual" using argument list ((ErrorType), XMPPError)

In MyModule, I have this listing:

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

In MyModuleTests:

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

doSomething returns an Observable .

Why am I getting this error message: Cannot invoke 'XCTAssertEqual' with an argument list ((ErrorType), MyError) ?

+5
source share
3 answers

You must add an extension corresponding to Equatable for your MyError.

 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; } } } 

And then it’s quite simple to confirm your mistake.

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

I am.

+7
source

As the error shows, here you are trying to compare (ErrorType) with MyError using XCTAssertEqual

1.Check why you get an error like (ErrorType) instead of ErrorType

2. Turn the comparison after converting (Type cast) ErrorType to MyError .

+1
source

Turns out ErrorType can't be directly compared:

 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 

To accomplish the same thing, I did the following:

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

Source: https://habr.com/ru/post/1258007/


All Articles