mockolo icon indicating copy to clipboard operation
mockolo copied to clipboard

Mock for Combine

Open keithcml opened this issue 2 years ago • 2 comments

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.

keithcml avatar Jan 17 '22 11:01 keithcml

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()

ffittschen avatar Feb 22 '22 16:02 ffittschen

I believe this was fixed in #186 ?

maxwellE avatar Apr 27 '22 16:04 maxwellE

@keithcml Can you confirm that it is fixed?

uhooi avatar Nov 23 '22 06:11 uhooi

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.

kernandreas avatar Feb 09 '23 16:02 kernandreas

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.

fummicc1 avatar May 03 '23 15:05 fummicc1

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.

fummicc1 avatar May 12 '23 15:05 fummicc1