Open sgrgrsn opened 5 years ago
@sgrgrsn Hi, thanks for the feedback.
We had several discussion on this topic when we started this project. The general problem is that it's really hard to come up with a decent default return values. Also, it can be a bit misleading when someone does not setup them, and the test fails (for some other reason than missing stubs).
As for the current solutions - what we came up with in current project is setting these default values in setUp
method of the test case. Then you can override them if particular test needs specific behaviour. You still needs to do that, but only once per test case (or even less if you make it a helper method).
As for other possible solutions - I can imagine adding and doing something like register<T>(defaultReturnValue: T, forStubsReturning: T.Type)
somewhere in the global scope. That would be a general solution, either in global space (shared between tests) or in the particular mock scope. What do you think makes more sense?
I would also need a second opinion here - @Przemyslaw-Wosko ?
Thanks for the quick reply @amichnia 👍
I get your point that it can be invisible if it fails silently when a stub is not implemented.
I like your idea of having a way to register the default return value for specific types. That will at least solve my needs in one line at the call site:
register(defaultReturnValue: Observable.empty(), forStubsReturning: Observable.self)
@sgrgrsn just one remark. Your observable is a generic type, which means two things:
Above would work more like :
// Specify exact type
register(defaultReturnValue: Observable.empty(), forStubsReturning: Observable<Int>.self)
It might solve the problem or not, depending how many different type of observables you have. So again, probably you will have to register as many times as many types you have.
Maybe we can make it even more generic, but that would require further investigation. Again, the problem is that Observable<Int>.empty()
is not the same type as Observable<String>.empty()
so matching that would be a problem.
Thanks for pointing out. I see - didn't think about that when I wrote my reply.
I think the solution is still alright, as the majority of my observables are click event so Observable<Void>
.
Hi there 👋
Thanks a lot for a really useful framework! It has saved me for a lot of boiler plate and maintenance in our test targets 🎉
I was wondering if it's possible to ignore methods and getters that aren't stubbed on a mock object. It would be useful in tests that don't depend on all of the methods and getters in the mock object.
In my particular case I have a view with a bunch of observables that are being subscribed to by my tested class, but the given test is only depending on the few Observables I'm testing. Right now I have to set up a Given clause for every getter my tested class is subscribing to. I'm just telling it to return an empty observable to make it ignore them. This turns out to be quite a few lines for my big protocols like this:
Is there a way to avoid having to set up a stub for all the used methods and getters, or is there any way to set a default return value for all methods or getters returning a specific type (eg.
Observable
)?Hope my questions is clear enough.
Thanks in advance!