Latest 0.4.0
Homepage https://github.com/valnoc/FataMorgana
License MIT
Platforms ios 9.0, requires ARC
Dependencies Mirage
Authors

[]()

FataMorgana generates mocks using Mirage library.

It uses Sourcery as a code generator and provides several templates and annotations.


Installation

  1. Install Sourcery. (I use homebrew variant).
  2. Install templates

Cocoapods

Add this line into your Podfile under a test target and run pod update

pod 'FataMorgana'

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.


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

miragePartial – generate partial mock

Annotations for a method

mirageSel – set alternative name

mirageSkip – skip this method while generating a class mock


Usage (Full info)

1. Set .sourcery.yml

sources:
  - ./FataExample/Services/
  - ./FataExample/Objects/
templates:
  - ./../Template/
output: 
  ./FataExampleTests/mocks/
args:
  testableModule: "FataExample"
  imports: "import CoreLocationnimport CoreData"
  returnEmptyArray: false
  returnEmptyDict: true
  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
  • testableModule – name of module you test. Usually, it is main module of your app.
  • imports – enumerate additional imports of modules
  • returnEmptyArray – return [] for an array by default
  • returnEmptyDict – return [:] for a dictionary by default

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() functions.

For example, method

protocol SecondService {
...
  func makeRandomPositiveInt() -> Int
...

will become

class MockSecondService: SecondService, Mock {
...
  let sel_makeRandomPositiveInt = "sel_makeRandomPositiveInt"
  func makeRandomPositiveInt() -> Int {
      return mockManager.handle(sel_makeRandomPositiveInt, withDefaultReturnValue: anyInt(), withArgs: nil) as! Int
  }
...
}

Pay attention to withDefaultReturnValue: anyInt()
You have to implement such functions somewhere in your project. For example, in Any.swift file.

func anyString() -> String {
    return "any"
}

func anyInt() -> Int {
    return 4
}

func anyDouble() -> Double {
    return 1.0
}

func anyStringArray() -> [String] {
    return ["any_array"]
}

Special cases

Same methods’ names

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)

Generated.

    let sel_foo3 = "sel_foo3"
    func foo3(number: NSNumber, closure: @escaping Closure1) {
        mockManager.handle(sel_foo3, withDefaultReturnValue: nil, withArgs: number, closure)
    }
    let sel_foo3str = "sel_foo3str"
    func foo3(string: String, closure: @escaping Closure1) {
        mockManager.handle(sel_foo3str, withDefaultReturnValue: nil, withArgs: string, closure)
    }

Skip methods

Annotation mirageSkip

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

    //sourcery: mirageSkip
    func skipMe() {
    }

License

FataMorgana is available under MIT License.

Latest podspec

{
    "name": "FataMorgana",
    "version": "0.4.0",
    "summary": "FataMorgana generates mocks using Mirage library.",
    "description": "FataMorgana generates mocks using Mirage library.nnIt uses Sourcery as a code generator and provides several templates and annotations.nnv0.4.0n  - added variables for classesn  - fixed "any" names of generic functionsnnv0.3.0n  - added variables for protocolsn  - fixed methods duplication with class inheritance/implementationn  - fixed several bugsnnv0.2.0n  - refactored templaten  - fixed several bugs with callRealFunc generationn  - both mirageMock and miragePartial generate now one file Mock{...}n  - avoid initializers generationn  - added temporary throws workaroundnnv0.1.0nt-tinitial",
    "homepage": "https://github.com/valnoc/FataMorgana",
    "license": "MIT",
    "authors": {
        "Valeriy Bezuglyy": "[email protected]"
    },
    "source": {
        "git": "https://github.com/valnoc/FataMorgana.git",
        "tag": "v0.4.0"
    },
    "platforms": {
        "ios": "9.0"
    },
    "requires_arc": true,
    "source_files": "Template/**/*{.stencil}",
    "dependencies": {
        "Mirage": [
            "~> 1.0"
        ]
    }
}

Pin It on Pinterest

Share This