Cuckoo icon indicating copy to clipboard operation
Cuckoo copied to clipboard

Don't crash when a method is not stubbed

Open rbeeger opened this issue 8 years ago • 3 comments

Looking at Cuckoo's MockManager I notice there is a private method named failAndCrash that is called in all sorts of bad situations as for example when a method that is called on a mock is not stubbed. Why does this method crash the whole testsuite by calling fatalError(message)? I would prefer to see all problems my tests might have after one run and not one after the other as I fix them. Also an update to the code might make adding some stubbing later on necessary. In that case I still want the other tests to run and to see from the test results that there is a local problem somewhere.

rbeeger avatar Feb 20 '17 19:02 rbeeger

We'd love to do something else than just crash, but if you don't stub the method, then the MockManager can't know what should it return. And since it has to return something, the only solution is to call fatalError which is () -> Never. If you can think of a more elegant solution, I'm all ears, as this is something we'd love to solve.

One workaround would be to use a spy with an instance of a stub that gets generated alongside the mock. It is basically a nop stub and if you instantiate the mock using the stub instance, it shouldn't crash (because it will call the real methods). But that will not tell you which methods are called and shouldn't be.

TadeasKriz avatar Feb 23 '17 14:02 TadeasKriz

Couldn't the default behavior be to automatically stub a method to return .thenDoNothing() and then instead of crashing, fail the test case with a message that an unexpected method was called. Similar to how kiwi provides null mocks

Maybe instead of the default behavior you could make it optional when declaring the mock

var mockMyClass = MockMyClasss().nullMock()

dsrees avatar Mar 15 '17 16:03 dsrees

When creating a mock, could it spy on the generated stub by default? We're finding that we're writing

let mockFoo = FooMock().spy(on: MockStub())

almost all of the time. Having this by default would be helpful and avoid unintentional crashes.

macduy avatar May 16 '17 11:05 macduy