Generic Function

XCTAssertEqual(_:_:_:file:line:)

Asserts that two values are equal.

Declaration

func XCTAssertEqual<T>(_ expression1: @autoclosure () throws -> T, _ expression2: @autoclosure () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) where T : Equatable

Parameters

expression1

An expression of type T, where T is Equatable.

expression2

An expression of type T, where T is Equatable.

message

An optional description of the failure.

file

The file in which failure occurred. Defaults to the file name of the test case in which this function was called.

line

The line number on which failure occurred. Defaults to the line number on which this function was called.

Discussion

Use this function to compare two non-optional values of the same type.

Listing 1

Comparing Non-Optional Values Of The Same Type

class FrameSizeTests: XCTestCase {
    func testInitialFrameSize() {
        let view = CustomView()
        let expectedSize = CGSize(width: 640.0, height: 480.0)
        XCTAssertEqual(view.frame.size, expectedSize, "Unexpected frame size.")
    }
}

Listing 1 creates a new instance of a custom class called CustomView, and checks that its size is equal to CGSize(width: 640.0, height: 480.0).

See Also

Testing for Equality and Inequality