This repository contains a simple test project to showcase conventions for injecting mocked dependencies to allow real unit testing.
- If a class
AService
just usesBService
, in our unit tests ofAService
will be code ofBService
be executed. - Changes of the implementation in
BService
can change test results ofAService
. - To avoid this, we inject a mock of
BService
when testingAService
- Inject dependency in initializer, with an default parameter if possible. This default value can also be provided by a factory class in the future.
- Always name it with
Protocol
as suffix - Put the protocol in a separate file in the same folder as the Service
- Always create protocols, to allow dependent classes to quickly mock it. Only create mocks of your class if needed
- Mocks have to be classes to be injected and changed from outside, namely our test functions.
- Mocks should be placed in the test target
- Mocks must have
Mock
as suffix. - Always Put mock variables directly above the specific function.
- Name those variables alwasys beginning with the function name
- Name the tested class variable
sut
(system under test) - Name functions like this
func testFunctionName_input_expectedOutput()