FataMorgana generates mocks using Mirage library.
It uses Sourcery as a code generator and provides its own template.
Using FataMorgana you can generate class and protocol mocks. Requires Sourcery 0.18.0 and Mirage 2.0+.
- Install Sourcery. (I use homebrew variant)
- Download Mirage and template
- Create sourcery.yml
- Add build phase for mocks generation
Add this lines into your Cartfile, run carthage update --platform iOS
and link binary to the target as you always do it)
github "valnoc/Mirage" ~> 2.0
github "valnoc/FataMorgana" ~> 2.0
The needed Mock.stencil is located in Template folder
Add this line into your Podfile under a test target and run pod update
pod 'FataMorgana' ~> 2.0
Podfile example
target 'MainTarget' do
...
target 'TestTarget' do
inherit! :search_paths
pod 'FataMorgana'
end
end
Copy /FataMoragana/Template folder into your project dir. Copy Mirage sources into your project dir.
- Set .sourcery.yml
- Add a build phase script to Tests target
- Mark objects to mock
- Add Any.swift file
mirageMock - generate mock
miragePartialMock - generate partial mock
mirageSel - set alternative name. mirageSel=myBestFunction
mirageSkip - skip this method while generating a class mock
mirageReturn - name for return function. mirageReturn=anyString()
sources:
- ./../Example
templates:
- ./../Template/
output:
./../Example/ExampleTests/mocks/generated
args:
imports:
- framework: Example
testable: true
- Foundation
returnOptionalAsNil: false
- sources
Enumerate sources folders
- templates
Set templates folder (Pods/FataMorgana)
- output
Set output folder for generated files. Each mock is generated into separate file.
- args
imports - enumerate additional imports of modules. Use testable: true if this module needs @testable prefix returnOptionalAsNil - return nil if return type is optional
sourcery --config "${SRCROOT}"
You can call sourcery several times with different configs if you need it.
Add annotation mirageMock before object to generate a mock of it (class or protocol).
//sourcery: mirageMock
protocol SecondService {
}
Add annotation miragePartial before object to generate a mock of it.
//sourcery: miragePartial
class FirstService {
}
All methods return a default value calling any<Return_Type>() functions. Implement such functions in Any.swift file.
Use annotation mirageSel
func foo3(number: NSNumber, closure: @escaping Closure1)
func foo3(string: String, closure: @escaping Closure1)
These methods will have same string selector generated for Mirage. In such cases you can provide an alternative name for a method using annotation
func foo3(number: NSNumber, closure: @escaping Closure1)
//sourcery: mirageSel=foo3str
func foo3(string: String, closure: @escaping Closure1)
Use annotation mirageSkip
Mark a method with this annotation if you don't want it to appear in mock.
//sourcery: mirageSkip
func skipMe() {
}
FataMorgana is available under MIT License.