Skip to content

Instantly share code, notes, and snippets.

@Pretz
Created January 28, 2015 21:39
Show Gist options
  • Save Pretz/1240f07b0edf06d87121 to your computer and use it in GitHub Desktop.
Save Pretz/1240f07b0edf06d87121 to your computer and use it in GitHub Desktop.
strongly typed mock model constructor for Swift tests
struct MockFile<T> {
let fileName: String
init(_ fileName: String) {
self.fileName = fileName
}
}
struct Mocks {
static let UserWithProfile = MockFile<User>("registrations_mock")
static let Locations = MockFile<[Location]>("locations_mock")
}
func model<T: Model>(file: MockFile<T>) -> T {
return T.self(fromResponseBody: jsonMock(file.fileName))
}
func models<T: Model>(file: MockFile<[T]>) -> [T] {
return T.modelsFromResponseBody(jsonMock(file.fileName)) as [T]
}
////
let user = model(Mocks.UserWithProfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment