princes-theatre-challenge

Prince's Theatre Challenge

View project on GitHub

DRY - Don’t Repeat Yourself

2020.12.10

Beautiful DRY BDD tests.

    // I love DRY - Dont Repeat Yourself!
    /// Perform fetch details and inspect the results.
    func doFetchDetailsAndInspect(movieService: MovieFinderServices, id: String) {
        var movie: MovieDetail? = nil
        var isDone = false
        self.cancellable = movieService.fetchMovieDetailBy(id: id)
            .sink { (error) in
                print("Done. Error=\(error)")
                isDone = true
            } receiveValue: { (result) in
                movie = result
                print("Movie:\n\(result)")
                expect(movie).toNot(beNil())
            }
        expect(isDone).toEventually(beTrue())
    }

Then this is reused everywhere :-)

        describe("MovieFinderService") {
            context("FilmWorld") {
                it("load movies from remote server") {
                    self.doFetchAndInspect(movieService: self.fwMovieService)
                }
                it("retrieves a movie's detail by id") {
                    self.doFetchDetailsAndInspect(movieService: self.fwMovieService, id: "fw2488496")
                }
            }
            context("CinemaWorld") {
                it("load movies from remote server") {
                    self.doFetchAndInspect(movieService: self.cwMovieService)
                }
                it("retrieves a movie's detail by id") {
                    self.doFetchDetailsAndInspect(movieService: self.cwMovieService, id: "cw2488496")
                }
            }
        }