Using Cuckoo with Swift, why is the call to `stub` complaining that the class from GeneratedMocks doesn't conform to Mock protocol?

Viewed 700

Using Cuckoo 1.4.1, I get the error

Global function 'stub(_:block:)' requires that 'DeepFileManager' conform to 'Mock'

I don't know how to proceed, because the instance passed to the stub function was created from the class in GeneratedMocks.swift. The reference to DeepFileManager is the class that is mocked.

So it's a mystery to me why Swift 4 would complain about the conformance of the superclass of the instance I've passed to it. Can you lead me out of this conundrum?

Here's the code for the test:

class RecordingTests: QuickSpec {
    override func spec() {
        let FAKE_ID = "String";
        let mockFileManager: MockDeepFileManager = MockDeepFileManager()
        let cut = Recording(id: FAKE_ID, date: nil, status: nil, device: nil, deviceId: nil, deviceName: nil, fileManager: mockFileManager)
        describe("A recording") {
            context("when it's not on disk") {
                it("responds that filePresent is false") {
                    stub(mockFileManager) {stub in
                        when(stub.fileExists(audioId: FAKE_ID)).then()
                    }
                    expect(cut.filePresent()).to(equal(false))
                    verify(mockFileManager).fileExists(audioId: "Matchable")
                }
            }
        }
    }

And here's the declaration of MockDeepFileManager from GeneratedMocks.swift:

 class MockDeepFileManager: DeepFileManager, Cuckoo.ClassMock {
1 Answers

As noted in the comment, an issue was submitted against the package, and the response explains how to make it work.

The problem in the code was calling then() (with no arguments), as then requires a closure function with a signature to match the signature of the stubbed function. A simpler alternative that would achieve the aim is thenReturn (instead of then), which just needs a value of the type of the stubbed function's return.

The maintainer of the Cuckoo package expressed sympathy that Swift issues an error about the protocol, instead pointing to the error in the invocation of then.

Related