Skip to content

Commit 88b6bc7

Browse files
committed
Fakes and the like
1 parent 77ea603 commit 88b6bc7

11 files changed

+51
-96
lines changed

README.md

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
11
A Pragmatic Approach to iOS Testing
22
===============
33

4-
An ebook about pragmatic testing strategies. Click below to get the latest epub.
4+
An ebook about pragmatic testing strategies. Click below to get the latest ePub.
55

6-
[ ![Image](assets/Cover.png "Pragmatic iOS Testing") ](https://github.com/orta/pragmatic-testing/blob/master/pragmatic_testing.epub?raw=true "Download epub")
6+
[ ![Image](assets/Cover.png "Pragmatic iOS Testing") ](https://github.com/orta/pragmatic-testing/blob/master/pragmatic_testing.epub?raw=true "Download ePub")
77

88
Or check [it on GitBook](https://www.gitbook.com/book/orta/pragmatic-ios-testing/details).
99

1010
Wanna contribute? [read this](CONTRIBUTING.md).
1111

1212
##### Existing Pages
1313

14-
| Topics | Last Updated | Length |
14+
| Topic | Last Updated | Length |
1515
| -------|----|-----|
1616
|[What Is/What And Why Of The Book](chapters/en-UK/what_is/what_and_why_of_the_book.md)|2016-03-31|Words: 518|
1717
|[What Is/How Can I Be Pragmatic With My Testing](chapters/en-UK/what_is/how_can_i_be_pragmatic_with_my_testing.md)|2016-03-31|Words: 370|
18-
|[XCTest/Useful Terminology](chapters/en-UK/xctest/useful_terminology.md)|2016-03-31|Words: 105|
19-
|[XCTest/What Is XCTest How Does It Work](chapters/en-UK/xctest/what_is_xctest_how_does_it_work.md)|2016-03-31|Words: 558|
18+
|[XCTest/What Is XCTest How Does It Work](chapters/en-UK/xctest/what_is_xctest_how_does_it_work.md)|2016-03-31|Words: 573|
2019
|[XCTest/Types Of Testing](chapters/en-UK/xctest/types_of_testing.md)|2016-03-31|Words: 216|
2120
|[XCTest/Unit Testing](chapters/en-UK/xctest/unit_testing.md)|2016-03-31|Words: 336|
2221
|[XCTest/Behavior Testing](chapters/en-UK/xctest/behavior_testing.md)|2016-03-31|Words: 1228|
2322
|[XCTest/Three Types Of Unit Tests](chapters/en-UK/xctest/Three_Types_of_Unit_Tests.md)|2016-03-31|Words: 314|
2423
|[XCTest/Test Driven Development](chapters/en-UK/xctest/test_driven_development.md)|2016-03-31|Words: 233|
2524
|[XCTest/Integration Testing](chapters/en-UK/xctest/integration_testing.md)|2016-03-31|Words: 536|
26-
|[XCTest/Testing Terminology](chapters/en-UK/xctest/testing_terminology.md)|2016-03-31|Words: 197|
2725
|[Foundations/Dependency Injection](chapters/en-UK/foundations/dependency_injection.md)|2016-03-31|Words: 806|
28-
|[Foundations/Stubs And Mocks](chapters/en-UK/foundations/stubs_and_mocks.md)|2016-03-31|Words: 200|
29-
|[Foundations/Fakes Plus Protocols ](chapters/en-UK/foundations/fakes__plus_protocols_.md)|2016-03-31|Words: 151|
26+
|[Foundations/Stubs Mocks And Fakes](chapters/en-UK/foundations/stubs_mocks_and_fakes.md)||Words: 475|
3027
|[OSS Libs/Expanding On Bdd Frameworks](chapters/en-UK/oss_libs/expanding_on_bdd_frameworks.md)|2016-03-31|Words: 3|
3128
|[OSS Libs/Mocking And Stubbing Ocmock And Ocmockito ](chapters/en-UK/oss_libs/mocking_and_stubbing__ocmock_and_ocmockito_.md)|2016-03-31|Words: 0|
3229
|[OSS Libs/Network Stubbing Ohttp And Vcrurlconnection](chapters/en-UK/oss_libs/network_stubbing__ohttp_and_vcrurlconnection.md)|2016-03-31|Words: 0|
@@ -59,9 +56,10 @@ Wanna contribute? [read this](CONTRIBUTING.md).
5956
|[Wrap Up/Twitter Follows](chapters/en-UK/wrap_up/twitter_follows.md)|2016-03-31|Words: 0|
6057
|[Wrap Up/Recommended Websites](chapters/en-UK/wrap_up/recommended_websites.md)|2016-03-31|Words: 51|
6158

62-
Over 200 words: 42.2%
63-
Over 300 words: 37.8%
59+
60+
Over 200 words: 47.6%
61+
Over 300 words: 42.9%
6462

6563
##### Generating the ebook
6664

67-
The latest epub copy can be generated by running `ruby generate.rb`
65+
The latest ePub copy can be generated by running `ruby generate.rb`

SUMMARY.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22

33
* [Chapters/En-Uk/What Is/What And Why Of The Book](chapters/en-UK/what_is/what_and_why_of_the_book.md)
44
* [Chapters/En-Uk/What Is/How Can I Be Pragmatic With My Testing](chapters/en-UK/what_is/how_can_i_be_pragmatic_with_my_testing.md)
5-
* [Chapters/En-Uk/Xctest/Useful Terminology](chapters/en-UK/xctest/useful_terminology.md)
65
* [Chapters/En-Uk/Xctest/What Is Xctest How Does It Work](chapters/en-UK/xctest/what_is_xctest_how_does_it_work.md)
76
* [Chapters/En-Uk/Xctest/Types Of Testing](chapters/en-UK/xctest/types_of_testing.md)
87
* [Chapters/En-Uk/Xctest/Unit Testing](chapters/en-UK/xctest/unit_testing.md)
98
* [Chapters/En-Uk/Xctest/Behavior Testing](chapters/en-UK/xctest/behavior_testing.md)
109
* [Chapters/En-Uk/Xctest/Three Types Of Unit Tests](chapters/en-UK/xctest/Three_Types_of_Unit_Tests.md)
1110
* [Chapters/En-Uk/Xctest/Test Driven Development](chapters/en-UK/xctest/test_driven_development.md)
1211
* [Chapters/En-Uk/Xctest/Integration Testing](chapters/en-UK/xctest/integration_testing.md)
13-
* [Chapters/En-Uk/Xctest/Testing Terminology](chapters/en-UK/xctest/testing_terminology.md)
1412
* [Chapters/En-Uk/Foundations/Dependency Injection](chapters/en-UK/foundations/dependency_injection.md)
15-
* [Chapters/En-Uk/Foundations/Stubs And Mocks](chapters/en-UK/foundations/stubs_and_mocks.md)
16-
* [Chapters/En-Uk/Foundations/Fakes Plus Protocols ](chapters/en-UK/foundations/fakes__plus_protocols_.md)
13+
* [Chapters/En-Uk/Foundations/Stubs Mocks And Fakes](chapters/en-UK/foundations/stubs_mocks_and_fakes.md)
1714
* [Chapters/En-Uk/Oss Libs/Expanding On Bdd Frameworks](chapters/en-UK/oss_libs/expanding_on_bdd_frameworks.md)
1815
* [Chapters/En-Uk/Oss Libs/Mocking And Stubbing Ocmock And Ocmockito ](chapters/en-UK/oss_libs/mocking_and_stubbing__ocmock_and_ocmockito_.md)
1916
* [Chapters/En-Uk/Oss Libs/Network Stubbing Ohttp And Vcrurlconnection](chapters/en-UK/oss_libs/network_stubbing__ohttp_and_vcrurlconnection.md)

chapters/en-UK/foundations/fakes__plus_protocols_.md

-13
This file was deleted.

chapters/en-UK/foundations/stubs_and_mocks.md

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
### Mocks
2+
3+
A mock object is an object created by a library to emulate an existing object's API. In general there are two main types of mocks
4+
5+
1. _Strict Mocks_ - or probably just Mocks. These objects will only respond to what you define upfront, and will assert if they receive anything else.
6+
7+
1. _Nice (or Partial) Mocks_ which wrap existing objects. These mocks objects can define the methods that they should respond too, but will pass any function / messages they haven't been told about to the original.
8+
9+
In Objective-C you can define mocks that act as specific instance of a class, conform to specific protocols or be a class itself. In Swift this is still all up in the air, given the language's strict type system.
10+
11+
### Stubs
12+
13+
A stub is a method that is replaced at runtime with another implementation. It is common for a stub to not call the original method. It's useful in setting up context for when you want to use known a return value with a method.
14+
15+
You can think of it as being method swizzling, really.
16+
17+
### Mocks and Stubs
18+
19+
From a personal opinion I avoid stubbing and mocking code which is under my control.
20+
21+
When you first get started, using Mocks and Stubs feel like the perfect tool for testing code, but it becomes unwieldy as it can quickly get out of sync with reality. They can be a great crutch, when you really can't figure out how to test something however.
22+
23+
A great example of when to use stubbing is when dealing with an Apple class that you cannot easily replace or use your own copy. For example I regularly use partial mocks of `UIScreen` instances in order to emulate being on an iPad simulator when it's actually running on an iPhone simulator. This saves us time from running our test suite twice, sequentially, on multiple simulators.
24+
25+
When you own the code that you're working with, it can often be easier to use a fake.
26+
27+
### Fakes
28+
29+
A Fake is an API compatible version of an object. That is it. Fakes are extremely easy to make in both Swift and Objective-C.
30+
31+
In Objective-C fakes can be created easily using the loose typing at runtime. If you create an object that responds to the same selectors as the one you want to fake you can pass it instead by typecasting it to the original.
32+
33+
In Swift the use of `AnyObject` is discouraged by the compiler, so the use of fudging types doesn't work. Instead, you are better off using a protocol. This means that you can rely on a different object conforming to the same protocol to make test code run differently. It provides a different level of coupling.
34+
35+
For my favourite use case of Fakes, look at the chapter on Network Models, or Testing Delegates.

chapters/en-UK/xctest/testing_terminology.md

-31
This file was deleted.

chapters/en-UK/xctest/useful_terminology.md

-16
This file was deleted.

chapters/en-UK/xctest/what_is_xctest_how_does_it_work.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,13 @@ It has three tests, that each test their own expectations.
4343
- The second ensures that when `popen(["cat", path])` is called, it returns a number of characters greater than `4096`
4444
- Finally the third one checks an expectation, and if it's wrong, it will faile the test.
4545

46-
47-
// ED NOTE: Should this hosted/not hosted live here? prob not
48-
4946
##### What is the difference between hosted test targets and unhosted
5047

5148
When talking pragmatically, we're really talking about writing tests against apps or libraries. Depending on whether you have dependencies on Cocoa or UIKit, you end up having to make a choice. Hosted, or not hosted.
5249

53-
The terminology has changed recently, a hosted test used to be known as Application Tests, and unhosted was known as Logic Tests. The older terminology gives a better hint at how the tests would be ran. A hosted test is ran inside your application after `application:didFinishLaunchingWithOptions:` has finished. This means there is afully running application context.
50+
The terminology has changed recently, a hosted test used to be known as Application Tests, and "unhosted" was known as Logic Tests. The older terminology gives a better hint at how the tests would be ran.
51+
52+
A hosted test is ran inside your application after `application:didFinishLaunchingWithOptions:` has finished. This means there is a fully running application, and your tests run with that happening around it. This gives you access to a graphics context, the application's bundle and other useful bits and pieces.
5453

5554
Un-hosted tests are useful if you're testing something very ephemeral/logical and relying only on Foundation, but anything related to UIKit/Cocoa subclasses will eventually require you to host the test bundle in an application. You'll see this come up every now and again when setting up test suites.
5655

generators/generate_toc.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ def create
1313
rest = readme.split(start_split)[1]
1414
finale = rest.split(end_split)[1]
1515

16-
template = start_split + "\n\n| Topics | Last Updated | Length | \n| -------|----|-----|\n"
16+
template = start_split + "\n\n| Topic | Last Updated | Length | \n| -------|----|-----|\n"
1717

1818
template = add_markdown_files_to template
19-
template += "\nOver 200 words: " + rough_completion_estimate[:covered] + "%"
19+
template += "\n\nOver 200 words: " + rough_completion_estimate[:covered] + "%"
2020
template += "\nOver 300 words: " + rough_completion_estimate[:solid] + "%\n"
2121

2222
new_file = start + template + "\n" + end_split + finale

generators/structure_of_pragmatic_programming.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22
chapters/en-UK/what_is/what_and_why_of_the_book.md
33
chapters/en-UK/what_is/how_can_i_be_pragmatic_with_my_testing.md
44

5-
chapters/en-UK/xctest/useful_terminology.md
65
chapters/en-UK/xctest/what_is_xctest_how_does_it_work.md
76
chapters/en-UK/xctest/types_of_testing.md
87
chapters/en-UK/xctest/unit_testing.md
98
chapters/en-UK/xctest/behavior_testing.md
109
chapters/en-UK/xctest/Three_Types_of_Unit_Tests.md
1110
chapters/en-UK/xctest/test_driven_development.md
1211
chapters/en-UK/xctest/integration_testing.md
13-
chapters/en-UK/xctest/testing_terminology.md
1412

1513
chapters/en-UK/foundations/dependency_injection.md
16-
chapters/en-UK/foundations/stubs_and_mocks.md
17-
chapters/en-UK/foundations/fakes__plus_protocols_.md
14+
chapters/en-UK/foundations/stubs_mocks_and_fakes.md
1815

1916
chapters/en-UK/oss_libs/expanding_on_bdd_frameworks.md
2017
chapters/en-UK/oss_libs/mocking_and_stubbing__ocmock_and_ocmockito_.md

pragmatic_testing.epub

-1.71 KB
Binary file not shown.

0 commit comments

Comments
 (0)