Quick icon indicating copy to clipboard operation
Quick copied to clipboard

Quick with Combine

Open skywalkerdude opened this issue 4 years ago • 4 comments

  • [x] I have read CONTRIBUTING and have done my best to follow them.

Hi team! I really love this library and am looking forward to incorporating it into my project. One quick (pun intended) question I have is regarding the framework that Apple released recently called Combine. I am trying to figure out a good way to integrate Quick/Nimble with Combine calls.

For example, I have something like this:

    func test_networkError() {

        // Stub mock to return a network error.
        URLProtocolMock.error = ErrorType.network(description: "network error!")

        let failureExpectation = expectation(description: "Invalid.failure")
        let finishedExpectation = expectation(description: "Invalid.finished")
        finishedExpectation.isInverted = true
        let receiveExpectation = expectation(description: "Invalid.receiveValue")
        receiveExpectation.isInverted = true

        let cancellable
            = sut.callMethod()
                .sink(
                    receiveCompletion: { (completion: Subscribers.Completion<ErrorType>) -> Void in
                        switch completion {
                        case .failure(let error):
                            XCTAssertEqual(error.localizedDescription, "Error occurred when making a network request")
                            failureExpectation.fulfill()
                        case .finished:
                            finishedExpectation.fulfill()
                        }
                        return
                },
                    receiveValue: { _ in
                        receiveExpectation.fulfill()
                        return
                })
        wait(for: [failureExpectation, finishedExpectation, receiveExpectation], timeout: testTimeout)
        cancellable.cancel()
    }

I'm not quite sure how to translate this into a test with quick/nimble (since we are making our assertions and expectations in the receiveCompletion/receiveValue callbacks). If you could point me in the right direction or add some documentation regarding how to integrate with Combine, I will greatly appreciate it!! :)

skywalkerdude avatar Apr 12 '20 07:04 skywalkerdude

        describe("dataTaskPublisher") {
            var session: URLSession!
            beforeEach {
                session = .init(configuration: .default)
            }

            context("success") {
                itBehavesLike(CombinePublisher.self) {
                    session
                        .dataTaskPublisher(for: URL(fileURLWithPath: #filePath))
                        .shouldReceive(numberOfTimes: 1)
                        .before(timeout: 10)
                }
            }

            context("failure retried sends same error") {
                itBehavesLike(CombinePublisher.self) {
                    session
                        .dataTaskPublisher(for: URL(string: "invalid")!)
                        .retry(2)
                        .mapError(verifyError)
                        .shouldFail(expectedError: VerifiedError.expected)
                        .before(timeout: 10)
                }
            }
        }

See this example

and other combine publishers, like:

            context("Just value") {
                itBehavesLike(CombinePublisher.self) {
                    Just("apple")
                        .shouldFinish(expectedValue: "apple")
                        .immediately
                }
            }

using our custom quick behavior for Combine publishers: https://github.com/sparta-science/Legible

paulz avatar May 15 '21 09:05 paulz

Hey there! 👋🏼

I'm a new maintainer for this project and I'm trying to get the next release out ASAP and also clean up old issues and old PRs. I'm closing all issues that no longer seem relevant or are very old / stale.

This does not mean this issue is necessarily being rejected. If you are still interested in contributing the changes specified in this issue, please comment to request it be reopened.

We appreciate you opening this issue and acknowledge the time and effort you put in to contribute! You're awesome. 💯 However, we are all volunteers here with limited capacity working for free. Unfortunately, that means we must close out stale issues and PRs to move the project forward in a reasonable way.

We apologize for the inconvenience and appreciate your understanding! 😎 ✌🏼

jessesquires avatar Apr 14 '22 22:04 jessesquires

@jessesquires I think @skywalkerdude proposal it's very valuable. Are there any way we could help? Could we agree with a DSL for testing publishers and create a PR adding those? Thanks in advice.

DanielRamosAcosta avatar Aug 23 '22 22:08 DanielRamosAcosta

I'm not sure whether we should do this. There are third party frameworks (Like Legible) that can test Combine publishers, and it feels like maybe this is an area for those to help out with?

Another reason why I'm unsure is that I'm not entirely sold on the DSL Legible provides. It certainly does the trick, but it doesn't feel like idiomatic Quick. Admittedly, I am written my own test helpers which also allows you to test Combine publishers with Quick (or XCTest for that matter), so I'm definitely biased there.

I am definitely open to more of a discussion on this, if there are strong opinions towards providing native Combine support, then sure.

younata avatar Aug 27 '22 22:08 younata