diff --git a/TUM Campus App.xcodeproj/project.pbxproj b/TUM Campus App.xcodeproj/project.pbxproj index 9115928a..5f44213e 100644 --- a/TUM Campus App.xcodeproj/project.pbxproj +++ b/TUM Campus App.xcodeproj/project.pbxproj @@ -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 */ @@ -257,6 +258,7 @@ 504C292B1E2BBB9C007583E2 /* MovieTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MovieTests.swift; sourceTree = ""; }; 504C292D1E2BBB9C007583E2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 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 = ""; }; + 50D153591E6DEAC9005EF930 /* MockReceiver.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MockReceiver.swift; sourceTree = ""; }; 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 = ""; }; /* End PBXFileReference section */ @@ -637,6 +639,7 @@ 504C292A1E2BBB9C007583E2 /* TUM Campus AppUnitTests */ = { isa = PBXGroup; children = ( + 50D153591E6DEAC9005EF930 /* MockReceiver.swift */, 504C292B1E2BBB9C007583E2 /* MovieTests.swift */, 504C292D1E2BBB9C007583E2 /* Info.plist */, ); @@ -967,6 +970,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 50D1535A1E6DEAC9005EF930 /* MockReceiver.swift in Sources */, 504C292C1E2BBB9C007583E2 /* MovieTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/TUM Campus AppUnitTests/MockReceiver.swift b/TUM Campus AppUnitTests/MockReceiver.swift new file mode 100644 index 00000000..3b36eb19 --- /dev/null +++ b/TUM Campus AppUnitTests/MockReceiver.swift @@ -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!) + } +} diff --git a/TUM Campus AppUnitTests/MovieTests.swift b/TUM Campus AppUnitTests/MovieTests.swift index 675e4053..ef45d299 100644 --- a/TUM Campus AppUnitTests/MovieTests.swift +++ b/TUM Campus AppUnitTests/MovieTests.swift @@ -1,4 +1,3 @@ -// // TUM_Campus_AppUnitTests.swift // TUM Campus AppUnitTests // @@ -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() } @@ -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 + 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 } } }