Instantly share code, notes, and snippets.

Embed
What would you like to do?
Notes from TDD iOS Notes

TDD Workshop Notes

http://tdd-workshop.uikonf.com

Twitter hashtag #tddberlin

Mobile Central Europe Conference in Warsaw in Feb 2015

Resources

Reading

book Michael Feathers - Working With Legacy Code

Blog post http://agilewarrior.wordpress.com/2012/10/06/its-not-about-the-unit-tests/

Tools

Examples

github.com/mobile-warsaw

Introduction to tests and testing framework

Pawel Dudek @eldudi

Unit test life cycle

Arrange - set up

Act - do something

Assert - check result

What a unit test is not

Read book Michael Feathers - Working With Legacy Code

  • talks to database, filesystem, network
  • edit config files or environment

Property of a good unit test

Isolated

Has to be FAST!

TDD

Write test first!

Gets you into the zone

Helps track work

Use your own API before someone else or the rest of the app

If it's hard to test, it's hard to use.

Reduce amount of QA and testing. You will still have bugs.

BDD

Work from the outside in

Use examples to clarify requirements

Specta

Specta https://github.com/specta/specta

Define specifications

Describe and context block

beforeEach - Arrange and Act

it - Assert

afterEarch - destroy created objects and state from beforeEach

Separation into beforeEach and it

focusing tests

fdescribe

fit

fcontext

pending

it(@"bla", PENDING)

Perfect setup

run tests automatically on each change of a file

xcpretty - simplify xcodebuild output to one . per test. builds faster

My questions

What does Specta do, that XCTest does not?

Red Green Factor

Aleksader Zubala @alekzubala

develop in short repeatable cycles

form hypothesis and check them

confidence to refactor

improve code quality

Red

What will move the project forward?

  1. Write test (object and method might not exist). AppCode makes it easy to create missing files, methods
  2. Test Fails

Green

Solution does not have to be the best one. Even hardcoding is ok.

Make test pass. Test the test.

Refactor

Improve solution. Acutally implement.

DRY - don't repeat yourself

Repeat

Red, Green, Refactor

What do you get

Baby steps

spend most time on refactoring

easy to identify what goes wrong

Common mistakes

move quickly through red, green phases

make cycles short

don't forget refactoring

Homework

Add correct index when opening swiping preview.

Types of Test

Return test, value test

When faking, mocking objects you might have to create categories. Only add those to the test target.

describe and context is used to structure the tests

describe -

context -

Interaction Test

Arrange

Act

Simulate

Assert

Mock, Spy

Stub, Fake

How to work with legacy code effectively

Maciej Oczko @maciejoczko

Creating a new spec file

File, new - File (not new class) - ClassToTestSpec.m

Add to to test target

type cspec + tab to create starting test code

Task

  • test if rightBarButtonItem changes correctly

Testing View related methods

View lifecycle methods (viewDidLoad etc.) are not called automatically. You have to call them yourself.

Be careful: calling -[UIViewcontroller view] will call loadView, viewDidLoad. But will not call viewWillAppear.

Approach

Find inflection point: narrow interface to a set of classes

example here: Singleton PollManager made testing difficult

  • break external dependencies with dependency injection (passing parameters e.g. initWith:)
  • break internal dependencies with extracting a small method

After that make changes.

Task

extract validation logic to test textfiel validation. lines 116-138

AppCode

cit is live template (code snippet) that expands. See Settings -> Live templates

alt+enter - create class, create missing methods etc.

alt+cmd+c - create constants from repeated strings

cmd+shift+t - switch from test to implementation class

alt+enter - dow what i need, like create new class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment