Skip to content

Instantly share code, notes, and snippets.

@binho
Created December 21, 2016 12:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binho/1bd7b43d1df6e1e8f6da8d1da4f7e736 to your computer and use it in GitHub Desktop.
Save binho/1bd7b43d1df6e1e8f6da8d1da4f7e736 to your computer and use it in GitHub Desktop.
Testing view model in XCTest
#import <XCTest/XCTest.h>
#import "Person.h"
#import "PersonViewModel.h"
@interface SimpleMVVMTests : XCTestCase
@property (nonatomic) NSString *salutation;
@property (nonatomic) NSString *fullName;
@property (nonatomic) NSDate *birthDate;
@end
@implementation SimpleMVVMTests
- (void)setUp {
[super setUp];
self.salutation = @"Mr";
self.fullName = @"Cleber";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"pt_BR"]];
self.birthDate = [dateFormatter dateFromString: @"1986-03-04"];
}
- (void)tearDown {
self.salutation = nil;
self.fullName = nil;
self.birthDate = nil;
[super tearDown];
}
- (void)testInitialization {
Person *person = [[Person alloc] initWithSalutation:self.salutation name:self.fullName birthDate:self.birthDate];
PersonViewModel *personViewModel = [[PersonViewModel alloc] initWithPerson:person];
XCTAssertNotNil(personViewModel, @"Person view model should not be nil");
XCTAssertTrue(personViewModel.person == person, @"The view model person should be equal o person passed in");
}
- (void)testUseSalutationIfAvailable {
Person *person = [[Person alloc] initWithSalutation:self.salutation name:self.fullName birthDate:self.birthDate];
PersonViewModel *personViewModel = [[PersonViewModel alloc] initWithPerson:person];
XCTAssertEqualObjects(personViewModel.nameText, @"Mr Cleber");
}
- (void)testShouldNotUseUnavailableSalutation {
Person *person = [[Person alloc] initWithSalutation:nil name:self.fullName birthDate:self.birthDate];
PersonViewModel *personViewModel = [[PersonViewModel alloc] initWithPerson:person];
XCTAssertEqualObjects(personViewModel.nameText, @"Cleber");
}
- (void)testShouldUseCorrectDateFormat {
Person *person = [[Person alloc] initWithSalutation:self.salutation name:self.fullName birthDate:self.birthDate];
PersonViewModel *personViewModel = [[PersonViewModel alloc] initWithPerson:person];
XCTAssertEqualObjects(personViewModel.birthDateText, @"Tuesday March 4, 1986");
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment