Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Add tests for movie manager #58

Merged
merged 5 commits into from
Mar 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions TUM Campus App.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
50200E261DF4992900B6FBE3 /* LectureDetail.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 50200E251DF4992900B6FBE3 /* LectureDetail.storyboard */; };
504C292C1E2BBB9C007583E2 /* MovieTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 504C292B1E2BBB9C007583E2 /* MovieTests.swift */; };
50AFC9861E6C8ECF00E7886A /* Pods_Campus.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 50AFC9851E6C8ECF00E7886A /* Pods_Campus.framework */; };
50D1535A1E6DEAC9005EF930 /* MockReceiver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50D153591E6DEAC9005EF930 /* MockReceiver.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -257,6 +258,7 @@
504C292B1E2BBB9C007583E2 /* MovieTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MovieTests.swift; sourceTree = "<group>"; };
504C292D1E2BBB9C007583E2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
50AFC9851E6C8ECF00E7886A /* Pods_Campus.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Pods_Campus.framework; path = "../../Library/Developer/Xcode/DerivedData/Campus_App-abdlonhukuicsjbpypswdmfoclts/Build/Products/Debug-iphonesimulator/Pods_Campus.framework"; sourceTree = "<group>"; };
50D153591E6DEAC9005EF930 /* MockReceiver.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MockReceiver.swift; sourceTree = "<group>"; };
B036FEAD3E1DFC9E65802F03 /* Pods-Campus.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Campus.release.xcconfig"; path = "Pods/Target Support Files/Pods-Campus/Pods-Campus.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -637,6 +639,7 @@
504C292A1E2BBB9C007583E2 /* TUM Campus AppUnitTests */ = {
isa = PBXGroup;
children = (
50D153591E6DEAC9005EF930 /* MockReceiver.swift */,
504C292B1E2BBB9C007583E2 /* MovieTests.swift */,
504C292D1E2BBB9C007583E2 /* Info.plist */,
);
Expand Down Expand Up @@ -967,6 +970,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
50D1535A1E6DEAC9005EF930 /* MockReceiver.swift in Sources */,
504C292C1E2BBB9C007583E2 /* MovieTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
25 changes: 25 additions & 0 deletions TUM Campus AppUnitTests/MockReceiver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// MockReceiver.swift
// TUM Campus App
//
// Created by Max Muth on 06.03.17.
// Copyright © 2017 LS1 TUM. All rights reserved.
//

import XCTest

@testable import Campus

class MockReceiver: TumDataReceiver {
let mockReceiveData: (([DataElement], XCTestExpectation) -> Void)?
let expectation: XCTestExpectation?

init(receiveData: @escaping (([DataElement], XCTestExpectation) -> Void), expectation: XCTestExpectation) {
self.mockReceiveData = receiveData
self.expectation = expectation
}

func receiveData(_ data: [DataElement]) {
self.mockReceiveData?(data, self.expectation!)
}
}
48 changes: 25 additions & 23 deletions TUM Campus AppUnitTests/MovieTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//
// TUM_Campus_AppUnitTests.swift
// TUM Campus AppUnitTests
//
Expand All @@ -11,12 +10,8 @@ import XCTest
@testable import Campus

class MovieTests: XCTestCase {

var movies = [Movie]()
var manager = TumDataManager(user: nil)

var testGetAllMoviesExpectation: XCTestExpectation?

override func setUp() {
super.setUp()
}
Expand All @@ -25,29 +20,36 @@ class MovieTests: XCTestCase {
super.tearDown()
}

func testGetAllMovies() {
self.testGetAllMoviesExpectation = expectation(description: "Fetch movies and check whether there are > 0 movies")
manager.getMovies(self)
waitForExpectations(timeout: 5) { error in
// if let error = error {
// XCTFail("waitForExpectationsWithTimeout errored: \(error)")
// }
func testReceivedDataAreMovies() {
func receiveData(_ data: [DataElement], expectation: XCTestExpectation) {
let movies = data.flatMap { $0 as? Movie }
XCTAssertTrue(data.count == movies.count, expectation.description)
expectation.fulfill()
}


let expect = expectation(description: "Received data items can be initialized to movies")
let tmpReceiver = MockReceiver(receiveData: receiveData, expectation: expect)

manager.getMovies(tmpReceiver)
waitForExpectations(timeout: 5) { error in }
}

}

extension MovieTests: TumDataReceiver {

func receiveData(_ data: [DataElement]) {
for element in data {
if let movieElement = element as? Movie {
movies.append(movieElement)
func testOnlyUpcomingMovies() {
func receiveData(_ data: [DataElement], expectation: XCTestExpectation) {
let movies = data.flatMap { $0 as? Movie }
let dateNow = Date()
let upcomingMovies = movies.filter() { movie in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future. If the method only has a trailing closure omit the () just like in flatMap a few lines up

return movie.airDate >= dateNow
}
XCTAssertTrue(movies.isEmpty || movies.count == upcomingMovies.count, expectation.description)
expectation.fulfill()
}
XCTAssertNotNil(movies)
self.testGetAllMoviesExpectation?.fulfill()

let expect = expectation(description: "Movies are all in the future or there are no movies")
let tmpReceiver = MockReceiver(receiveData: receiveData, expectation: expect)

manager.getMovies(tmpReceiver)
waitForExpectations(timeout: 5) { error in }
}

}