0
0
iOS Swiftmobile~10 mins

Test-driven development in Swift in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a test case class in Swift.

iOS Swift
import XCTest

class [1]: XCTestCase {
}
Drag options to blanks, or click blank then click option'
AUnitTest
BMyTestCase
CTestClass
DTestCase
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class name that does not inherit from XCTestCase
Forgetting to import XCTest
2fill in blank
medium

Complete the code to write a test method that checks if 2 + 2 equals 4.

iOS Swift
func testAddition() {
    XCTAssertEqual([1], 4)
}
Drag options to blanks, or click blank then click option'
A2 + 2
B1 + 2
C2 + 3
D3 + 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using an expression that does not equal 4
Forgetting to use XCTAssertEqual
3fill in blank
hard

Fix the error in the test method declaration to make it a valid test method in Swift.

iOS Swift
func [1]() {
    XCTAssertTrue(true)
}
Drag options to blanks, or click blank then click option'
ATestMethod
BverifyMethod
CcheckMethod
DtestMethod
Attempts:
3 left
💡 Hint
Common Mistakes
Starting method name with uppercase 'Test'
Using names that do not start with 'test'
4fill in blank
hard

Fill both blanks to create a test that fails if the value is not nil.

iOS Swift
func testValueIsNil() {
    let value: String? = nil
    XCTAssert[1](value [2] nil)
}
Drag options to blanks, or click blank then click option'
ATrue
B==
C!=
DEqual
Attempts:
3 left
💡 Hint
Common Mistakes
Using XCTAssertEqual instead of XCTAssertTrue
Using '!=' instead of '==' for nil comparison
5fill in blank
hard

Fill all three blanks to write a test that verifies a function returns true for positive numbers.

iOS Swift
func testIsPositive() {
    let number = 5
    let result = isPositive([1])
    XCTAssert[2](result == [3])
}
Drag options to blanks, or click blank then click option'
Anumber
BTrue
Ctrue
Dnumber + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an incorrect argument to the function
Using XCTAssertFalse instead of XCTAssertTrue
Using uppercase 'True' instead of lowercase 'true'