Swift Package Index


A spunky mocking library for Swift


  • The latest stable release is 0.2.2. Released over 1 year ago.
  • The last commit to master was over 1 year ago.

Swift Version Compatibility

  • 0.2.2 and master
    5.3
    5.2
    5.1
    5.0
    4.2
Full build results

Platform Compatibility

  • 0.2.2 and master
    iOS
    macOS(Intel)
    macOS(ARM)
    Linux
    tvOS
    watchOS
Full build results

Moxie

Build Status License Pod

A spunky mocking library for Swift

Moxie

Using Moxie

Let's say you have a protocol you want to mock.

protocol List {
    mutating func add(_ item: String)
    mutating func clear()

    func get(_ index: Int) -> String?
}

You can use Moxie to create a mock.

import Moxie

struct MockList: Mock, List {
    var moxie = Moxie()

    mutating func add(_ item: String) {
        record(function: "add", wasCalledWith: [item])
    }

    mutating func clear() {
        record(function: "clear")
    }

    func get(_ index: Int) -> String? {
        return value(forFunction: "get")
    }
}

Then you can use it in your tests.

func testList() {

    var mockList = MockList()

    // verifying invocations
    mockList.add("one")
    mockList.clear()

    XCTAssertTrue(mockList.invoked(function: "add"))
    XCTAssertEqual("one", mockList.parameters(forFunction: "add")[0] as? String)
    XCTAssertTrue(mockList.invoked(function: "clear"))

    // stubbing
    mockList.stub(function: "get", return: "first")

    XCTAssertEqual("first", mockList.get(0))
}

Documentation

  1. Installation
  2. Creating Mocks
  3. Stubbing
  4. Verifying Invocations