Trouble mocking ProducerScope

I have a function i need to write a Unit test for:

override suspend fun subscribeToPagedJourneys(journeyCount: Int,
                                              scoreType: Int,
                                              startDate: String,
                                              subscribe: Boolean): Flow<Output<List<JourneyEntity>>> {
    return coroutineHelper.channelFlow {
        if (subscribe) {

        if (journeyRepository.canGetPagedJourneys(this)) {
            journeyRepository.getPagedJourneys(this, journeyCount, scoreType, startDate)

I need to verify that journeyRepository.subscribe(this) is called and to do that i need to mock ‘this’ which is


I’ve really struggled finding the solution for this, its most likely ignorance on my part but I’ve had no responses on stack over flow so came here.

You don’t mock the instance under test, you mock its dependencies. You want to mock journeyRepository and then will you verify that subscribe is called on the mock with myInstanceUnderTest as the argument.

‘this’ is not the class instance its ProducerScope<Output<List<JourneyEntity>>>.

In the unit tests i launch all the coroutine tests runBlocking so their scope is CoroutineScope. There doesn’t seem to be any functionality to mock, or initialise a ProducerScope without calling channelFlow with a supplied block to fulfil the call.

Yeah, I missed that the argument is a different “this”, but you still mock journeyRepository and then verify that subscribe is called on the mock.

Verifying that subscribe is called requires mocking journeyRepository, nothing else. There’s no need to mock the ProducerScope based on what you’ve described.

If you do actually need to mock or spy the scope, then mock coroutineHelper and provide your own ProducerScope mock when channelFlow is invoked.