diff --git a/custom-words.txt b/custom-words.txt index 204a23155..6a85508de 100644 --- a/custom-words.txt +++ b/custom-words.txt @@ -73,6 +73,7 @@ SDLC Serilog signtool signup +Sourcery sqlcmd struct structs diff --git a/docs/architecture/mobile-clients/ios/index.md b/docs/architecture/mobile-clients/ios/index.md index 46c3b31cd..6feca23ea 100644 --- a/docs/architecture/mobile-clients/ios/index.md +++ b/docs/architecture/mobile-clients/ios/index.md @@ -444,3 +444,51 @@ This makes it convenient to switch between these files or open them side-by-side mode, dark mode, and with a large dynamic type size. ⚠️ These tests are done using an **iPhone 15 Pro (17.0.1)** simulator, otherwise tests may fail because of subtle differences between iOS versions. + +### Mocks generation + +We use [Sourcery](https://github.com/krzysztofzablocki/Sourcery) for automatic mock generation. + +In order to automatically generate a mock from a protocol, just add a comment with +`// sourcery: AutoMockable` to such protocol, perform a build and the mock will be automatically +generated and added to the `AutoMockable.generated.swift` file. + +For example: + +```swift +protocol FooProtocol { // sourcery: AutoMockable + func bar() -> Bool +} +``` + +:::info Manual generation + +There are some cases where the automatically generated mock does not cover the mock scenario we want +or it cannot handle some closure types, specially in function's parameters. In such cases prefer +create the mock manually and remove the protocol's comment as `AutoMockable`. + +::: + +#### Custom annotations + +Sourcery allows us to annotate different parts of our code to guide code generation. On top of this +custom annotations have been added in `AutoMockable.stencil` to handle special cases. + +- **useSelectorName**: Method annotation used to indicate that the generated mocked properties need + to use the selector name instead of the short method name. This is specially useful when using + function overloading where we need the mocked names to also have the parameters names to + differentiate between the different mocked functions. +- **mockReceivedInvocations**: Method annotation used to indicate that we want to generate the + mocked property to store an array of the received invocations of the parameters passed each time + the function is called. + +For example: + +```swift +protocol FooProtocol { // sourcery: AutoMockable + func bar(fooParameter: String) -> Bool + func bar(anotherParameter: Int) -> Bool // sourcery: useSelectorName + func saveNumber(theNumber: Int) -> Bool // sourcery: mockReceivedInvocations + func annotateMultiple(fooParameter: String) // sourcery: useSelectorName, mockReceivedInvocations +} +```