A plugin for AppCode to generate mock classes automatically.
- Open AppCode
- Open AppCode → Preferences
⌘,
- Go to Plugins
- Click Browse repositories...
- Search for 'Swift Mock Generator for AppCode'
- Click Install
- Restart AppCode
- Create a mock class inheriting from from protocol(s) you would like to mock.
- With the cursor inside the class declaration, press
alt ↩
. - Select 'Generate mock'.
- To regenerate the mock, place the cursor anywhere inside the mock and select 'Generate mock' again.
protocol Animator {
func animate(duration: TimeInterval, animations: () -> (), completion: (Bool) -> ()) -> Bool
}
class MockAnimator: Animator {
<generate mock from here>
}
class Object {
var animator: Animator = ...
func methodToTest() { ... }
}
let mockAnimator = MockAnimator()
let objectUnderTest = Object()
objectUnderTest.animator = mockAnimator
Provide the mock with a return result and it will return it.
mockAnimator.stubbedAnimateResult = true
Closures without parameters are executed automatically so no need to stub the animations
closure.
Closures with parameters will not be executed unless you provide their values.
mockAnimator.stubbedAnimateCompletionResult = (true, ())
objectUnderTest.methodUnderTest()
XCTAssert(mockAnimator.invokedAnimate)
XCTAssertEqualWithAccuracy(mockAnimator.invokedAnimateParameters.duration, 0.25, accuracy: 0.01)
XCTAssertEqual(mockAnimator.invokedAnimateCount, 2)
XCTAssertEqualWithAccuracy(mockAnimator.invokedAnimateParametersList[0].duration, 0.25, accuracy: 0.01)
- Captures invocation status of a method.
- Captures invoked method parameters.
- Stub values for your mocks to return.
- Automatically calls closure parameters with stubbed values.
- Supports mocks conforming to one or or many protocols.
- Handles overloaded method declarations.
- Regenerate your mock in one action.
- Supports associated types.
- Respects public mocks and makes queries publicly available.
- Records multiple invocations of methods and their parameters.