Test-Driven iOS Development with Swift 4 by Dominik Hauser

Test-Driven iOS Development with Swift 4 by Dominik Hauser

Author:Dominik Hauser
Language: eng
Format: epub
Publisher: Packt Publishing
Published: 2017-10-30T06:58:34+00:00


Fake objects

Ideally, a unit test should test one microfeature and nothing else. However, in object-oriented programming (OOP), objects talk to each other, exchange data, and react to the changes of their neighbors. As a result, when writing a test, it is often difficult to isolate one specific module from another. Without isolation, a test does not test just one microfeature, but many.

To isolate modules from each other, we can use a concept called fake objects. Fake objects act as placeholders for real objects or modules, but they are controlled by test code. This means a test sets up fake objects, controls their behavior, and tests whether the system under the test reacts as expected.

The most important fake objects are mocks, stubs, and fakes. These are explained as follows:

Mocks: They act as recorders. They register whether the system under a test calls the expected methods of another instance with expected arguments. For example, if we have class A that should call method b() of class B, when something happens, we would create a mock for B that sets a Boolean value to true in case b() is called. In the test, we use this Boolean value to assert whether b() has been called.

Stubs: These are used when we need defined return values from a method. In a test, it is often useful to have a fixed hardcoded return value for a method that the system under the test calls. The test then asserts that the system under test reacts in an expected way to the defined return value. This makes it easy to test many different scenarios without complicated setups.

Fakes: They act as stand-ins for real objects that a system under test communicates with. They are needed to make the code compile, but they are not needed to assert that something expected has happened. Fakes are often used when they are easier to set up than the real objects, or when we need to make sure that the test is independent of the implementation of the real object.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.