ninokierulf / work-notes

My collection of learning and solutions to encountered issues
0 stars 0 forks source link

[Book] Pro iOS Testing: XCTest Framework for UI and Unit Testing #13

Open ninokierulf opened 1 year ago

ninokierulf commented 1 year ago

By: Avi Tsadok 2020

Writing Tests

func checkPersonValues(  
    person: Person,  
    firstName : String,  
    lastName : String,   
    line : UInt = #line, // <--
    file : StaticString = #file // <-- 
) {  
    XCTAssertEqual(person.firstName, firstName, file: file, line:line)
    XCTAssertEqual(person.lastName, lastName, file: file, line:line)
}       

Test Doubles

    - Dummy - Does nothing. Purpose: to fill the gap of instantiating objects
    - Fake - always returns the same value
    - Stub - control return value
    - Spy - does not return anything. To record calls that can be asserted  later
    - Mock - assertion happens in Mock via `verify()` via `Bool`

Comparing

Parameterized Unit Tests

func testGenerateLayout_abstractMethod1() { var events = [Event]() events.append(Event(...)) // different data set ...

let expectedLayout = LayoutStructure()

runTest(withData: events, expectedLayout: LayoutStructure())  // <--

}

func testGenerateLayout_abstractMethod2() { var events = [Event]() events.append(Event(...)) ...

let expectedLayout = LayoutStructure()

runTest(withData: events, expectedLayout: LayoutStructure()) //  <--

}

- Loading test cases from a file
      - effortless to add more tests cases
      - easy to validate 
      - anybody can write
      - excellent for cross-platform testing
      - `Bundle(for: type(of: self)).path(forResource: "tests", ofType: "json")`
```json
{
   "test":[
      {
         "name":"test1",
         "events":[
            {
               "startDate":"04/27/2020 10:00",
               "endDate":"04/27/2020 11:00"
            },
            {
               "startDate":"04/27/2020 10:30",
               "endDate":"04/27/2020 11:20"
            },
            {
               "startDate":"04/28/2020 12:30",
               "endDate":"04/28/2020 12:30"
            }
         ],
         "expectedStructure":"--==--"
      },
      {
         "name":"test2",
         "events":[
            {
               "startDate":"04/27/2020 10:30",
               "endDate":"04/27/2020 11:30"
            },
            {
               "startDate":"04/27/2020 10:45",
               "endDate":"04/27/2020 11:50"
            },
            {
               "startDate":"04/28/2020 12:20",
               "endDate":"04/28/2020 15:30"
            }
         ],
         "expectedStructure":"--==--"
      }
   ]
}

<reading> expect more </reading>