|
| 1 | +# Swift Mock Generator Xcode Source Editor Extension |
| 2 | + |
| 3 | +An Xcode extension (plugin) to generate mock classes automatically. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +[Looking for the AppCode version?](https://github.com/seanhenry/MockGenerator) |
| 8 | + |
| 9 | +**IMPORTANT:** |
| 10 | +- This plugin is in development and only supports a fraction of the features offered in the [AppCode plugin](https://github.com/seanhenry/MockGenerator). |
| 11 | +- Only tested with Xcode 8.3.2 but will support Xcode 9 soon. |
| 12 | + |
| 13 | +## Install Swift Mock Generator Xcode Source Editor Extension |
| 14 | + |
| 15 | +- Download the latest release [here](https://github.com/seanhenry/SwiftMockGeneratorForXcode/releases) |
| 16 | +- Copy the app to the `Applications` folder. |
| 17 | +- Open the app |
| 18 | +- Select the path to your project ([Why do I have to do this?](##why-do-i-have-to-set-a-path-to-my-project?)) |
| 19 | +- Open Xcode |
| 20 | + |
| 21 | +## How to create a new Swift mock |
| 22 | + |
| 23 | +- Create an empty mock class conforming to a protocol. |
| 24 | + |
| 25 | +Example: |
| 26 | +``` |
| 27 | +class MyMock: MyProtocol { |
| 28 | +} |
| 29 | +``` |
| 30 | +- Place the cursor inside the class declaration. |
| 31 | +- Click `Editor -> Mock Generator -> Generate Mock`. |
| 32 | + |
| 33 | +## How to recreate a Swift mock |
| 34 | + |
| 35 | +If you change the underlying protocol its mock will need to be regenerated. |
| 36 | + |
| 37 | +To regenerate the mock, simply follow the steps above. |
| 38 | + |
| 39 | +## Recommended: assign a shortcut |
| 40 | + |
| 41 | +- Select preferences `⌘,` in Xcode. |
| 42 | +- Choose 'Key Bindings'. |
| 43 | +- Search for 'Mock Generator'. |
| 44 | +- Choose your preferred shortcut. I prefer `⌃⌥⌘M`. |
| 45 | + |
| 46 | +## Recommended: use source control |
| 47 | + |
| 48 | +The mock generator will replace anything that is currently in your mock class with the generated mock. |
| 49 | + |
| 50 | +Undo is supported for Xcode plugins but you're safer to use source control in the event of unexpectedly generating a mock. |
| 51 | + |
| 52 | +## Features |
| 53 | + |
| 54 | +| Feature | Xcode | AppCode |
| 55 | +|---|---|---| |
| 56 | +| Captures invocation status of methods.|✅|✅| |
| 57 | +| Captures invocation status of properties.||✅| |
| 58 | +| Captures invoked method parameters.||✅| |
| 59 | +| Stubs values for your mocks to return.|✅|✅| |
| 60 | +| Stubs a default value for return values where possible.||✅| |
| 61 | +| Automatically calls closure parameters with stubbed values.||✅| |
| 62 | +| Supports mocks conforming to one or many protocols.||✅| |
| 63 | +| Handles overloaded method declarations.||✅| |
| 64 | +| Regenerate your mock in one action.|✅|✅| |
| 65 | +| Supports associated types.||✅| |
| 66 | +| Supports parameter type-annotation attributes and inout.||✅| |
| 67 | +| Respects public and open mocks and makes queries publicly available.||✅| |
| 68 | +| Records multiple invocations of methods.|✅|✅| |
| 69 | +| Records multiple invocations of method parameters.||✅| |
| 70 | +| Generate mocks from protocols in 3rd party frameworks.||✅| |
| 71 | +| Generate mocks from classes||| |
| 72 | + |
| 73 | +## Feature requests |
| 74 | + |
| 75 | +As shown above, the AppCode plugin is much more feature-rich. If there is a feature you need, check for an existing GitHub issue and make a comment or, if no issue exists, raise a new issue. |
| 76 | + |
| 77 | +## Disable or remove the plugin |
| 78 | + |
| 79 | +To disable: |
| 80 | + |
| 81 | +Go to `System Preferences -> Extensions` and deselect the extension under `Xcode Source Editor`. |
| 82 | + |
| 83 | +To remove: |
| 84 | + |
| 85 | +Delete the app. |
| 86 | + |
| 87 | +## Nomenclature |
| 88 | + |
| 89 | +Despite being called a Mock Generator, this plugin actually generates something closer to a spy and stub. The word 'mock', whilst not technically correct, has been used because test doubles such as spies, mocks, and stubs have become colloquially known as mocks. |
| 90 | + |
| 91 | +## Why do I have to set a path to my project? |
| 92 | + |
| 93 | +The plugin uses SourceKit which needs a list of your Swift files to index. |
| 94 | + |
| 95 | +You have to set the path to your code because there is no way to derive it from an Xcode extension. |
0 commit comments