Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ccabanero/90c6e2aadfd4efa6b059333edeb2b314 to your computer and use it in GitHub Desktop.
Save ccabanero/90c6e2aadfd4efa6b059333edeb2b314 to your computer and use it in GitHub Desktop.
Sample iOS Unit Tests: Testing Model Class Initialization
// example of unit test for model initialization
// TEST CODE
import XCTest
@testable import yourmodule
class PlaceTest: XCTestCase {
let expectedCity = "Seattle"
let expectedState = "Washington"
let expectedImageURL = "https://seattle/tour/gumwall.png"
let expectedDescription = "Lots of sun LOL"
let expectedPopulation = 652405
let expectedLatitude = 47.606209
let expectedLongitude = -122.332071
let expectedPointsOfInterest = [PointOfInterest]()
var systemUnderTest: Place!
override func setUp() {
super.setUp()
systemUnderTest = Place(city: expectedCity, state: expectedState, imageURL: expectedImageURL, description: expectedDescription, population: expectedPopulation, latitude: expectedLatitude, longitude: expectedLongitude, pointsOfInterest: expectedPointsOfInterest)
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testSUT_InitializesCityName() {
XCTAssertEqual(systemUnderTest.city, expectedCity)
}
func testSUT_InitializesStateName() {
XCTAssertEqual(systemUnderTest.state, expectedState)
}
func testSUT_InitializesImageURL() {
XCTAssertEqual(systemUnderTest.imageURL, expectedImageURL)
}
func testSUT_InitializesDescription() {
XCTAssertEqual(systemUnderTest.description, expectedDescription)
}
func testSUT_InitializesPopulation() {
XCTAssertEqual(systemUnderTest.population, expectedPopulation)
}
func testSUT_InitializeLatitude() {
XCTAssertEqual(systemUnderTest.latitude, expectedLatitude)
}
func testSUT_InitializeLongitude() {
XCTAssertEqual(systemUnderTest.longitude, expectedLongitude)
}
func testSUT_InitializesPointsOfInterest() {
XCTAssertNotNil(systemUnderTest.pointsOfInterest)
XCTAssert(systemUnderTest.pointsOfInterest === expectedPointsOfInterest)
}
}
// PRODUCTION CODE
import Foundation
class Place {
let city: String
let state: String
let imageURL: String
let description: String
let population: Int
let latitude: Double
let longitude: Double
let pointsOfInterest: [PointOfInterest]
init(city: String, state: String, imageURL: String, description: String, population: Int, latitude: Double, longitude: Double, pointsOfInterest: [PointOfInterest]) {
self.city = city
self.state = state
self.imageURL = imageURL
self.description = description
self.population = population
self.latitude = latitude
self.longitude = longitude
self.pointsOfInterest = pointsOfInterest
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment