TDD Flashcards

1
Q

TDD steps

A

It has four steps:

  1. Write a failing test
  2. Make the test pass
  3. Refactor
  4. Repeat

Red: Write a failing test, before writing any app code.

Green: Write the bare minimum code to make the test pass.

Refactor: Clean up both your app and test code.

Repeat: Do this cycle again until all features are implemented.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Test conventions

A
  • XCTest: Requires all test methods begin with test to be run.
  • test: Followed by the name of the method being tested. Here, this is init. There’s then an underscore to separate it from the next part.

Optionally, if special set up is required, this comes next. This test doesn’t include this. If provided, this likewise is followed by an underscore to separate it from the last part.

Lastly, this is followed by the expected outcome or result. Here this is createsCashRegister.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

A few things you might look to refactor in refactor state in TDD

A

Duplicate logic

Comments

Code smells

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Three parts of test

A

Given a certain condition…

When a certain action happens…

Then an expected result occurs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

In which order are test methods executed?

A

The order in which test classes and test methods are run is not explicitly defined and cannot be relied upon.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Lifecycle of XCTestCase

A

XCTestCase subclass lifecycles are managed outside the test execution, and any class-level state is persisted between test methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When setUp() and tearDown() are executed?

A

After and before each test

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Organisation of test target folder

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

@testable attribute

A

Xcode provides a way to expose data types for testing without making them available for general use

This makes symbols that are open, public, and internal available to the test case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Not compiling counts as a failure?

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Use tests to guide … what?

A

Use tests to guide refactoring code for readability and performance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Functonal view controller testing is done by?

A

Functional testing is done by using separate methods for interacting with the UI (callbacks, delegate methods, etc.) from logic methods (updating state).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Using the host app

A

you have access to the UIApplication object and the whole View hierarchy in the tests.

One alternate way of retrieving and testing a view controller can be done as follows: First, get a reference to the storyboard:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Turn on Randomized order

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Code coverage

A

Coverage doesn’t mean the code works, but lack of coverage means that it’s not tested. For views and view controllers, it’s not expected to get to 100% coverage because TDD does not include UI testing. When you combine unit tests with UI automation tests, then you should expect to be able to cover most if not all of these files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Debugging tests

A

Make sure:

You have the right assumptions in the given statements.

Your then statements accurately reflect the desired behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Using test breakpoints

A
18
Q

XCTest expectations have two parts:

A

the expectation and a waiter. An expectation is an object that you can later fulfill. The wait method of XCTestCase tells the test execution to wait until the expectation is fulfilled or a specified amount of time passes.

19
Q

Testing for true asynchronicity

A

Create custom class that observe if text/color/font changes when “when” part happens in test and fulfill expectation within this class

20
Q

What expectation can expect?

A

NotificationCenter
exp.fulfill()

pro

21
Q

expectedFulfillmentCount

A

You can use
expectedFulfillmentCount to set how many times you have to fulfill exp

22
Q

Expecting something not to happen

A

exp.isInverted = true
you can invert expectation

23
Q

What you can add to Notification?

A

You can add data through userInfo[:]

24
Q

Expectation can be set. How?

A
25
Q

You can check an array of expectations with order

A

enforceOrder parameter

26
Q

Describe exceptions and what they can do

A

fulfill
Notifications
Arrays
invert
set fulfilling many times

Use XCTestExpectation and its subclasses to make tests wait for asynchronous process completion.

Test expectations help test properties of the asynchronicity, like order and number of occurrences, but XCTAssert functions should still be used to test state.

27
Q

Remove item from array

A
28
Q

XCTKVOExpectation and XCTNSPredicateExpectation

A
29
Q

Type of test doubles

A

Stub: Stubs stand in for the original object and provide canned responses. These are often used to implement one method of a protocol and have empty or nil returning implementations for the others.

Fake: Fakes often have logic, but instead of providing real or production data, they provide test data. For example, a fake network manager might read/write from local JSON files instead of connecting over a network.

Mock: Mocks are used to verify behavior, that is they should have an expectation that a certain method of the mock gets called or that its state was set to an expected value. Mocks are generally expected to provide test values or behaviors.

Partial mock: While a regular mock is a complete substitution for a production object, a partial mock uses the production code and only overrides part of it to test the expectations. Partial mocks are usually a subclass or provide a proxy to the production object.

30
Q

Mock static

A
31
Q

How to test view?

A

Subclass view that you want to test and override logic

Then inject this view to your SUT (view controller)

Make some changes on view controller

Asset what is needed

32
Q

Test doubles let you

A

test code in isolation from other systems, especially those that are part of system SDKs, rely on networking or timers.

33
Q

Mocks let you

A

swap in a test implementation of a class, and partial mocks let you just substitute part of a class.

34
Q

Fakes let you

A

supply data for testing or use in Simulator.

35
Q

Testing for time dependencies

A

During tests, use a very short timer (e.g., one millisecond instead of one second).

Swap the timer for a mock that executes the callback immediately.

Use the callback directly, and save the timing for an app or user-acceptance testing.

36
Q

How to use mockes?

A

Inject mock to your SUT and test what you want

You need create abstraction layer for your dependency (mock).
Use protocol

Test how SUT is reacting on your mock

37
Q

When to register cells for table view?

A

on did set

38
Q

How to dequeue different cells in table view?

A

You can use different method for dequeueing for each cell

39
Q

How to configure cell with viewModel?

A
40
Q
A