Last active
May 12, 2016 19:00
-
-
Save ccabanero/93501b0cc78e2023f119 to your computer and use it in GitHub Desktop.
Sample iOS Unit Tests: Confirming Model object instantation of a NSManagedObject subclass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <UIKit/UIKit.h> | |
| #import <XCTest/XCTest.h> | |
| #import "SightingReport.h" | |
| #import "SightingReport+CoreData.h" | |
| #import "AppDelegate.h" | |
| @interface SightingReportTest : XCTestCase | |
| //for accessing CoreData NSManagedObjectContext in assertions | |
| @property (nonatomic, strong) AppDelegate *appDelegate; | |
| @property (nonatomic, strong) NSManagedObjectContext *managedObjectContext; | |
| //system under test (i.e. model class) | |
| @property (nonatomic, strong) SightingReport *modelUnderTest; | |
| @end | |
| @implementation SightingReportTest | |
| - (void)setUp { | |
| [super setUp]; | |
| //for accessing CoreData NSManagedObjectContext | |
| self.appDelegate = [[UIApplication sharedApplication] delegate]; | |
| self.managedObjectContext = [self.appDelegate managedObjectContext]; | |
| //instantiate system under test | |
| self.modelUnderTest = [SightingReport createNewSightingReportWithCommonName:@"Mediterranean white snail" inManagedObjectContext:self.managedObjectContext]; | |
| } | |
| - (void)tearDown { | |
| //delete test data | |
| [SightingReport deleteSightingReportForUUID:self.modelUnderTest.uniqueId inManagedObjectContext:self.managedObjectContext]; | |
| self.modelUnderTest = nil; | |
| self.managedObjectContext = nil; | |
| self.appDelegate = nil; | |
| [super tearDown]; | |
| } | |
| - (void)testModelInstantiatesNewSightingReport { | |
| XCTAssertNotNil(self.modelUnderTest, @"Model under test fails to instantiate a new instance"); | |
| } | |
| //simply assert against actual vs. expected values (ignore the domain specific attributes) | |
| - (void)testModelSetsCommonNameCorrectlyForNewSightingReport { | |
| NSString *actualCommonName = self.modelUnderTest.commonName; | |
| NSString *expectedCommonName = @"Mediterranean white snail"; | |
| XCTAssertEqualObjects(actualCommonName, expectedCommonName, @"Model under test fails to create new SightingReport with correct common name."); | |
| } | |
| - (void)testModelSetsScientificNameCorrectlyForNewSightingReport { | |
| NSString *actualScientificName = self.modelUnderTest.scientificName; | |
| NSString *expectedScientificName = @"Cernuella virgata"; | |
| XCTAssertEqualObjects(actualScientificName, expectedScientificName, @"Model under test fails to create new SightingReport with correct scientific name."); | |
| } | |
| - (void)testModelSetsGroupCorrectlyForNewSightingReport { | |
| NSString *actualGroup = self.modelUnderTest.group; | |
| NSString *expectedGroup = @"Aquatic Animals"; | |
| XCTAssertEqualObjects(actualGroup, expectedGroup, @"Model under test fails to create new SightingReport with correct group name."); | |
| } | |
| - (void)testModelSetsKingdomCorrectlyForNewSightingReport { | |
| NSString *actualKingdom = self.modelUnderTest.kingdom; | |
| NSString *expectedKingdom = @"Animal"; | |
| XCTAssertEqualObjects(actualKingdom, expectedKingdom, @"Model under test fails to create new SightingReport with correct kingdom."); | |
| } | |
| - (void)testModelSetsUUIDForNewSightingReport { | |
| XCTAssertNotNil(self.modelUnderTest.uniqueId, @"Model under test fails to create a universaly unique identifier for new Sighting Report"); | |
| XCTAssert([self.modelUnderTest.uniqueId length] > 0, @"Model under test fails to create a universaly unique identifier for new Sighting Report"); | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment