FataMorgana 2.0.1

FataMorgana 2.0.1

Maintained by Valeriy Bezuglyy.



  • By
  • Valeriy Bezuglyy

FataMorgana

License GitHub issues

Cocoapods release Carthage compatible GitHub release

FataMorgana generates mocks using Mirage library.

It uses Sourcery as a code generator and provides its own template.

Features

Using FataMorgana you can generate class and protocol mocks. Requires Sourcery 0.18.0 and Mirage 2.0+.


Installation

  1. Install Sourcery. (I use homebrew variant)
  2. Download Mirage and template
  3. Create sourcery.yml
  4. Add build phase for mocks generation

Carthage

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

Cocoapods

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

Source files

Copy /FataMoragana/Template folder into your project dir. Copy Mirage sources into your project dir.


Usage (Short info)

  1. Set .sourcery.yml
  2. Add a build phase script to Tests target
  3. Mark objects to mock
  4. Add Any.swift file

Annotations for a class/protocol

mirageMock - generate mock

miragePartialMock - generate partial mock

Annotations for a method

mirageSel - set alternative name. mirageSel=myBestFunction

mirageSkip - skip this method while generating a class mock

mirageReturn - name for return function. mirageReturn=anyString()


Usage (Full info)

1. Set .sourcery.yml

sources:
  - ./../Example
templates:
  - ./../Template/
output: 
  ./../Example/ExampleTests/mocks/generated
args:
  imports: 
    - framework: Example
      testable: true
    - Foundation
  returnOptionalAsNil: false
  1. sources

Enumerate sources folders

  1. templates

Set templates folder (Pods/FataMorgana)

  1. output

Set output folder for generated files. Each mock is generated into separate file.

  1. args

imports - enumerate additional imports of modules. Use testable: true if this module needs @testable prefix returnOptionalAsNil - return nil if return type is optional

2. Add a build phase script to Tests target

sourcery --config "${SRCROOT}"

You can call sourcery several times with different configs if you need it.

3. Mark objects to mock

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 {
}

4. Add Any.swift file

All methods return a default value calling any<Return_Type>() functions. Implement such functions in Any.swift file.

Special cases

Same methods' names

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)

Skip methods

Use annotation mirageSkip

Mark a method with this annotation if you don't want it to appear in mock.

//sourcery: mirageSkip
func skipMe() {
}

License

FataMorgana is available under MIT License.