Giter VIP home page Giter VIP logo

fatamorgana's Introduction

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.

fatamorgana's People

Contributors

valnoc avatar

Stargazers

 avatar

Watchers

 avatar  avatar

fatamorgana's Issues

Проблема при работе с generics

в подобном случае ССС<[T]> неизвестен тип для mockAAA

lazy var mockAAA = FuncCallHandler<BBB, CCC<[T]>>(returnValue: ...)
func aaa( request: BBB) -> CCC<[T]> {
...
}

При генерации Partial Мока не работает mirageSel

при генерации частичного мока с помощью конструкции
//sourcery: miragePartial
и добавлении
//sourcery: mirageSel=foo3str
для функции func foo3(string: String, closure: @escaping Closure1)
в созданном моке было сгенерировано имя mock_foo3
При изменении //sourcery: miragePartial на //sourcery: mirageMock
и оставив //sourcery: mirageSel=foo3str без изменения, было сгенерировано имя mock_foo3str

Error mock generating

when i wrote someone like this

func function(arg1: Int, _ arg2: Int) {}

in generated mock produced this error

super. func(arg1: args![0]! as! Int, : args![1] as! Int)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.