mockolo
mockolo copied to clipboard
Mock for Combine
I have tried to annotate with the following for my AnyPublisher
/// @mockable(combine: publisher = CurrentValueSubject)
But the generator produces PassthroughSubject as
public private(set) var publisherSubject: PassthroughSubject...
I would expect there is a CurrentValueSubject instead.
We are currently facing the same issue. After investigating it a bit, it only fails to generate the mock as a CurrentValueSubject
, if it can't find any default value. E.g. in the following example, mockolo will generate a fooPublisherSubject = PassthroughSubject<FooEnum, Never>()
and a barSubject = CurrentValueSubject<Bool, Never>(false)
public enum FooEnum {
case one, two, three
}
/// @mockable(combine: fooPublisher = CurrentValueSubject; barPublisher = CurrentValueSubject)
public protocol SomeStream: AnyObject {
var fooPublisher: AnyPublisher<FooEnum, Never> { get }
var barPublisher: AnyPublisher<Bool, Never> { get }
}
In these lines of code, mockolo will decide to fall back to a PassthroughSubject
in case it can't find a default value.
As mockolo is already capable of adding parameters to the init
method in the case of assigning an AnyPublisher to a property, we might be able to extend it to add parameters for types of the CurrentValueSubjects as well?
Alternatively, we could probably add a new case to the CombineType
to allow overwriting the AnyPublisher
as if it were a normal variable, i.e. making mockolo backwards compatible to versions before PR #155. Then we could simply do this:
var someStream = SomeStreamMock()
let someSubject = CurrentValueSubject<FooEnum, Never>(.one)
someStream.fooPublisher = someSubject.eraseToAnyPublisher()
I believe this was fixed in #186 ?
@keithcml Can you confirm that it is fixed?
Just ran in the same situation. Thank you @ffittschen for the investigation. How did you work around in your code base?
Btw. #186 does not fix the root cause. It adds a new flag to disable Subject synthesis for all AnyPublisher properties.
I try to fix this issue by adding a new metadata to set a default value of CurrentValueSubject in https://github.com/uber/mockolo/pull/234.
If you have any advice, please let me know.
After reviewing my idea, I believe we can resolve this issue by using the --disable-combine-default-values
option. Therefore, I'll be closing this PR. Please feel free to reopen it whenever you wish to continue discussing this topic.