Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
56 lines (42 sloc) 1.13 KB

Creating mocks

All mock objects must conform to the Mock protocol and have an instance of Moxie.

import Moxie

class MockClass: Mock {
    var moxie = Moxie()
}

Additionally, all mock objects must be of the same type as the object being mocked. This can be accomplished via inheritence or adoption of the mocked object's protocol. If the object being mocked is not a protocol type (such as a struct), then it cannot be extended and so it must be mocked via protocol adoption.

Inheritance

import Moxie

class ExampleClass {
    func foo() -> String {
       return "this is an example"
    }
}

class MockClass: ExampleClass, Mock {
    var moxie = Moxie()

    override func foo() -> String {
        return "this is a different example"
    }
}

Protocol adoption

import Moxie

protocol ExampleProtocol {
    func foo() -> String
}

struct ExampleStruct: ExampleProtocol {
    func foo() -> String {
        return "this is an example"
    }
}

struct MockExampleStruct: ExampleProtocol, Mock {
    var moxie = Moxie()

    func foo() -> String {
        return "this is a different example"
    }
}
You can’t perform that action at this time.