Database Reference
In-Depth Information
override
func
setUp() {
super
.
setUp
()
coreDataStack
=
TestCoreDataStack
()
campSiteService
=
CampSiteService
(managedObjectContext:
coreDataStack
.
mainContext
!, coreDataStack:
coreDataStack
)
}
override
func
tearDown() {
super
.
tearDown
()
campSiteService
=
nil
coreDataStack
=
nil
}
}
This looks very similar to the previous test class. As your suite of tests expands and
you notice common or repeated code, you can refactor the tests as well as the
application code. You can feel safe doing this because the unit tests will fail if you
mess anything up! :]
Add a new method to test adding a campsite. This looks and works like the method
for testing the creation of a new camper:
func
testAddCampSite() {
let
campSite =
campSiteService
.
addCampSite
(
1
,
electricity:
true
, water:
true
)
XCTAssertTrue
(campSite.
siteNumber
==
1
,
"Site number should be 1"
)
XCTAssertTrue
(campSite.
electricity
.
boolValue
,
"Site should have electricity"
)
XCTAssertTrue
(campSite.
water
.
boolValue
,
"Site should have water"
)
}
You also want to check that the context is saved during this method, so add
another method to test for that. This method should also look familiar:
func
testRootContextIsSavedAfterAddingCampsite() {
let
expectRoot =
self
.
expectationForNotification
(
NSManagedObjectContextDidSaveNotification
, object:
coreDataStack
.
rootContext
) {
notification
in