mathonsunday
12/8/2015 - 11:06 PM

testing.swift

//: Playground - noun: a place where people can play


import Cocoa

import Foundation

// WinterBuddies a social network to connect you with people who can help you survive the winter
class ProfileViewController : UIViewController {
    let musicService = MusicService()
    init(musicService: MusicService) {
        self.musicService = musicService
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        musicService.play() // start playlist on application
    }
}

// ProfileViewControllerTests.swift
func testShouldPlayMusicWhenViewDidLoad() {
    // setup mock
    class MockMusicService : MusicService {
        var musicPlayed = false
        override func play() {
            musicPlayed = true
        }
    }

    var viewController = ProfileViewController(musicServicer: MockMusicService())

    // invoke
    viewController.viewDidLoad()

    // verify
    XCTAssertTrue(mockMusicService.playCalled)
}

class MockMusicService: MusicService {

    class func basic() -> MusicService {
        return builder().build()!
    }

    class func builder() -> MusicServiceBuilder {
        return MusicServiceBuilder()
            .withApplication("Spotify")
            .withPlaylist("Chill Party")
    }

}

extension  MusicServiceBuilder {

    func withApplication(application: String) -> MusicServiceBuilder {
        self.application = application
        return self
    }

    func withPlaylist(playlistName: String) -> MusicServiceBuilder {
        self.playlistName = playlistName
        return self
    }
}