Code Monkey home page Code Monkey logo

extensionmock's Introduction

ExtensionMock Macro - Requires Swift 5.10 Toolchain for Nested Protocols

The traditional approach to writing clean and testable code involves a few repetitive steps:

  • Write a protocol.
  • Implement the protocol.
  • Create a mock based on the protocol for testing.
  • When you change a method signature in one place, you must update the other two accordingly.

Does maintaining all this boilerplate by hand and having it clutter up our code sound ideal? What if we could concentrate on the actual implementation and let automation take care of the rest?

With the ExtensionMock macro, you can embrace a cleaner code-writing process:

  • Implement your functionality within an extension of the actual class.
  • The macro automatically generates the relevant nested protocol.
  • The macro creates a nested mock class to accompany it.
  • Now, whenever you alter the method signature in the real implementation, the protocol and mock are updated automatically.

Extension Mock

In many Swift projects, a single class might conform to one protocol. But Swift allows us the flexibility to write as many extensions as we wish—which is precisely what we will do, adhering to SOLID principles.

That’s where our ExtensionMock comes into play, with its nested protocols for a clean extension mock setup.

Example Code - Original

class Networking {}

@ExtensionMock
extension Networking: Networking.UserService {
    func getUser(_ id: String) async throws -> User  {
        User(id: "1", name: "real user")
    }
}

@ExtensionMock
extension Networking: Networking.ItemService {
    func getItem(_ id: String) async throws -> Item  {
        Item(id: "1", title: "real item")
    }
}

Example Code - Macro Generated

extension Networking {
    protocol UserService {
        func getUser(_ id: String) async throws -> User
    }
    final class UserServiceMocked: UserService {
         var getUserMock = MockAsyncThrowing<String, User>()
         func getUser(_ id: String) async throws -> User  {
            try await getUserMock.record((id))
        }
    }
}

extension Networking {
    protocol ItemService {
        func getItem(_ id: String) async throws -> Item
    }
    final class ItemServiceMocked: ItemService {
         var getItemMock = MockAsyncThrowing<String, Item>()
         func getItem(_ id: String) async throws -> Item  {
            try await getItemMock.record((id))
        }
    }
}

Example Code - Testing

let mock = Networking.UserServiceMocked()
mock.getUserMock.block = { User(id: "1", name: "fake user") }

extensionmock's People

Contributors

sisoje avatar

Stargazers

Martin avatar Muhammad Umer avatar  avatar Miguel Maia avatar Tunko avatar James Phipps avatar Bach avatar

Watchers

 avatar

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.