MockSwift allows you to write mocks and make better tests. Because MockSwift is an open source library 100% written in Swift, it is AVAILABLE ON ALL PLATFORMS. Initially MockSwift is inspired by Mockito.
The default strategy is to find behaviour defined with given(). If no behaviour is found, it will return a local stub. If no local stub is found, it will return a global stub.
@Mock private var service: UserService
// equivalent to
@Mock(strategy: .default)
private var service: UserService
// equivalent to
@Mock(strategy: [.given, .localStubs, .globalStubs])
private var service: UserService
You can change the order of the strategy list or remove items as you want.
Write mocks
Automatically
MockSwift provides a stencil template for sourcery. You can use the AutoMockable annotation to generate code.
Welcome to MockSwift
MockSwift allows you to write mocks and make better tests. Because MockSwift is an open source library 100% written in Swift, it is AVAILABLE ON ALL PLATFORMS.
Initially MockSwift is inspired by Mockito.
Table of Contents
Installation
Swift Package Manager
MockSwift has been designed to work with Swift Package Manager.
Usage
Quick Look
Details
Suppose that you have a
UserService
protocol.And you want to test this
UserCore
class.Make better tests
Now, with MockSwift, you can use a mocked
UserService
in your tests with the@Mock
annotation.And easly configure it to fully test
UseCore
.Given
given()
enables you to define behaviours.example:
you can also define behaviours when you instantiate the mock.
Then
then()
enables you to verify calls.example:
You can go further and verify order of calls
Stubs
In MockSwift, stubs are default values that are returned when no behaviours has been found.
Global Stubs
You can define a global stub for any type. It will concern all mocks you will use in every tests.
Local Stubs
You can also define a stub localy for any type. It will concern only the current mock.
Strategy
The default strategy is to find behaviour defined with
given()
. If no behaviour is found, it will return a local stub. If no local stub is found, it will return a global stub.You can change the order of the strategy list or remove items as you want.
Write mocks
Automatically
MockSwift provides a stencil template for sourcery. You can use the
AutoMockable
annotation to generate code.To generate code at every build, you can add a build phase before
Compile Sources
.Manually
To enable MockSwift for UserService type, you have to extend Mock.
To allow behaviour definition through
given()
method, you have to extend Given.To allow call verification through
then()
method, you have to extend Then.Documentation
If you need more details about the API, you can check out our API documentation.
Contribution
Would you like to contribute to MockSwift? Please read our contributing guidelines and code of conduct.
License
MockSwift is released under the MIT license. See LICENSE for details.
Credits