Skip to content

Instantly share code, notes, and snippets.

@novinfard
Last active March 16, 2020 16:55
Show Gist options
  • Save novinfard/9842fa4a28f19e8087fb9b937a3eba41 to your computer and use it in GitHub Desktop.
Save novinfard/9842fa4a28f19e8087fb9b937a3eba41 to your computer and use it in GitHub Desktop.
[Standing Unit Tests - MCT Article]
class StandingsEndpointLocalRequest: StandingsEndpointRequest {
let tournamentId: Int32
let file: String
init(file: String, tournamentId: Int32) {
self.file = file
self.tournamentId = tournamentId
}
override func urlRequest() -> URLRequest {
let file = Bundle(for: StandingsEndpointLocalRequest.self).url(forResource: self.file, withExtension: "json")
return URLRequest(url: file!)
}
}
class StandingsDatasourceTests: XCTestCase {
func testParsing() {
// Given
let datasource = self.testDatasourceFetchStandings()
// When
UtilitiesForTest.requestAndWaitSuccess(test: self, dataSource: datasource)
let standings = datasource.fetchedResultsController?.fetchedObjects as? [Standing]
// Then
XCTAssertNotNil(standings)
XCTAssertEqual(standings!.count, 3, "There should be 3 standings")
}
func testDuplicating() {
// Given
let datasource = self.testDatasourceFetchStandings()
// When
UtilitiesForTest.requestAndWaitSuccess(test: self, dataSource: datasource)
UtilitiesForTest.requestAndWaitSuccess(test: self, dataSource: datasource)
let standings = datasource.fetchedResultsController?.fetchedObjects as? [Standing]
// Then
XCTAssertEqual(standings!.count, 3, "There should be 3 standings")
}
func testGroupFiltering() {
// Given
let datasource = self.testDatasourceFetchStandings(groupName: "Group B")
// When
UtilitiesForTest.requestAndWaitSuccess(test: self, dataSource: datasource)
let standings = datasource.fetchedResultsController?.fetchedObjects as? [Standing]
// Then
XCTAssertEqual(standings!.count, 2, "There should be 2 standings for `Group B`")
}
}
extension StandingsDatasourceTests {
private func testDatasourceFetchStandings(
file: String = "tournamentStandingsSample.json",
tournamentId: Int32 = 10,
groupName: String? = nil) -> StandingsDatasource {
let memoryCDC = MemoryCoreDataController()
let dataController = MemoryRequestDataConnectionController(manage:memoryCDC.mainManagedObjectContext)
let entityRequest = StandingsEntityRequest(tournamentId: tournamentId, groupName: groupName)
let endpointRequest = StandingsEndpointLocalRequest(file: file, tournamentId: tournamentId)
return StandingsDatasource(
endpointRequest: endpointRequest,
entityRequest: entityRequest,
managedObjectContext: memoryCDC.mainManagedObjectContext,
dataController: dataController
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment