Swiftpack.co - Package - dooZdev/template-sourcery

templates to be used with sourcery

Why the need for this project

Did you know?

Swift has no runtime. So mocking or stubbing is harder and different. When writing tests you want answers to


What would you like to test?

  • is my function getting called?
    • what arguments are passed to a function
    • can I make my function throw
    • return value
  • immutable variables
  • ...

How many variables we have to manually maintain to write these tests?


struct Service {
 
  func fetch(_ done: @escaping () -> Void) {
  
  }
}

What do we need to test this?


1 add protocol

protocol ServiceProtocol {
func fetch(_ done: @escaping () -> Void)
}

struct Service: ServiceProtocol {
 
  func fetch(_ done: @escaping () -> Void) {
  
  }
}

subject under test = sut

would be then be a real object

Service would be a Mock

mocked objects have a defined behaviour


var service: ServiceMock = ServiceMock()
var sut: Caller = Caller()

func testServiceUpdate_success() {
  sut.update()

  XCTAssertTrue(service.fetchCalled, "")

}


ServiceMock - manual

struct ServiceMock: ServiceProtocol {
 
  var fetchCalled: Bool = false

  func fetch(_ done: @escaping () -> Void) {
    fetchCalled = true
  }
}

1 variable is not enough

var fetchCalled: Bool = false
var fetchCount: Int = 0
var fetchArguments: (()->Void)? 
var fetchClosure: ((()->Void) -> Void)?
  • Is this only called once?
  • Is the thing a do in done() called?
  • Does it go into the error flow when service returns an error?

To have an answer to all of these testing needs with the help of sourcery to generate code for mock and protocols and highway to run sourcery twice we can generate code for both protocols and mocks in one go.

What do the templates do

  • for any protocol marked with // sourcery:AutoMockable the mocked file is generated
  • optionally you could generate the protocol too. For every generated protocol a mock is generated automatically.

⚠️ warning

It is not because you can generate code that you have to write more code, just because it is generated!

Please keep writing small and delightful Types and do not over use protocols.

Where to start

You can have a look at ExampleCode and run the unit tests. They will test the generated code for both mocks and protocols.

Go ahead and add a protocol in the ExampleCode folder and mark it with // sourcery:AutoMockable and see the output in GeneratedMock


Thanks to!

github sourcery

Github

link
Stars: 0

Dependencies

Used By

Total: 0

Releases

Templates with correct spaces and tests - 2020-03-17 14:47:14

Stable version with:

  • templates split into separate files for readability
  • unit tests in Tests test the output in files
  • ExampleCode demonstrates the use

Generate Mocks in separate files - 2020-02-04 11:04:35

Some bugs are fixed

  • Imports are added from test target
  • imports added in-between tags // imports:begin & // imports:end
  • mocks are no longer in a very nasty long file, but get generated

bug fix

When a target did not import SourceryAutorProtocols types where not recognised and you could not generate mocks from annotations.

deprecating

  • no support for objective-c anymore

Clean - 2020-01-31 11:37:34

swift 5.1 - 2019-07-26 12:20:45

Protocol template can handle default arguments and support swift 5.0 - 2019-07-22 13:10:06

Beta stable release - 2019-06-06 19:14:29

Fix minor issues and add SourceryAutoProtocols via SwiftPM - 2019-03-07 12:20:33

- 2018-12-29 11:51:10

Generate Mocks - 2018-12-29 11:29:23