#import <XCTest/XCTest.h>
- (void)setUp { [super setUp]; // Put setup code here. This method is called before the invocation of each test method in the class. }
- (void)tearDown { // Put teardown code here. This method is called after the invocation of each test method in the class. [super tearDown]; }
- (void)testExample { // This is an example of a functional test case. XCTAssert(YES, @"Pass"); } - (void)testPerformanceExample { // This is an example of a performance test case. [self measureBlock:^{ // Put the code you want to measure the time of here. }]; }
All tests run independently.
Do maximum class isolation
The name of the test should reflect its purpose.
The result of the test should not affect the main code
- (void)testAlwaysFailed { /* 1 - */ XCTFail(@"always failed"); }
- (void)testIsEqualPrimitive { /* 1 - 1 2 - 2 3 - - */ int primitive1 = 5; int primitive2 = 5; XCTAssertEqual(primitive1, primitive2, @"(%d) equal to (%d)", primitive1, primitive2); }
- (void)testIsNotEqualPrimitive { /* 1 - 1 2 - 2 3 - - */ int primitive1 = 5; int primitive2 = 6; XCTAssertNotEqual(primitive1, primitive2, @"(%d) not equal to (%d)", primitive1, primitive2); }
- (void)testIsEqualWithAccuracyPrimitive { /* 1 - 1 2 - 2 3 - 4 - - */ float primitive1 = 5.012f; float primitive2 = 5.014f; float accuracy = 0.005; XCTAssertEqualWithAccuracy(primitive1, primitive2, accuracy, @"(%f) equal to (%f) with accuracy %f", primitive1, primitive2, accuracy); }
- (void)testIsNotEqualWithAccuracyPrimitive { /* 1 - 1 2 - 2 3 - 4 - - */ float primitive1 = 5.012f; float primitive2 = 5.014f; float accuracy = 0.001; XCTAssertNotEqualWithAccuracy(primitive1, primitive2, accuracy, @"(%f) not equal to (%f) with accuracy %f", primitive1, primitive2, accuracy); }
- (void)testIsTrue { /* 1 - boolean 2 - - */ BOOL isTrue = YES; XCTAssertTrue(isTrue); }
- (void)testIsFalse { /* 1 - boolean 2 - - */ BOOL isTrue = NO; XCTAssertFalse(isTrue); }
- (void)testIsNil { /* 1 - 2 - - */ id foo = nil; XCTAssertNil(foo, @"pointer:%p", foo); }
- (void)testIsNotNil { /* 1 - 2 - - */ id foo = @""; XCTAssertNotNil(foo); }
- (void)testGreaterPrivitive { /* 1 - 1 2 - 2 3 - - */ int privitive1 = 4; int privitive2 = 3; XCTAssertGreaterThan(privitive1, privitive2); }
- (void)testGreaterOrEqualPrivitive { /* 1 - 1 2 - 2 3 - - */ int privitive1 = 4; int privitive2 = 4; XCTAssertGreaterThanOrEqual(privitive1, privitive2); }
- (void)testLessPrivitive { /* 1 - 1 2 - 2 3 - - */ int privitive1 = 3; int privitive2 = 4; XCTAssertLessThan(privitive1, privitive2); }
- (void)testLessOrEqualPrivitive { /* 1 - 1 2 - 2 3 - - */ int privitive1 = 4; int privitive2 = 4; XCTAssertLessThanOrEqual(privitive1, privitive2); }
- (void)testThrowException { /* 1 - // 2 - - */ void (^block)() = ^{ @throw [NSException exceptionWithName:NSGenericException reason:@"test throw" userInfo:nil]; }; XCTAssertThrows(block()); }
- (void)testNoThrowException { /* 1 - // 2 - - */ void (^block)() = ^{ }; XCTAssertNoThrow(block()); }
@interface MyException : NSException @end @implementation MyException @end
- (void)testThrowExceptionClass { /* 1 - // 2 - 3 - - */ void (^block)() = ^{ @throw [MyException exceptionWithName:NSGenericException reason:@"test throw" userInfo:nil]; }; XCTAssertThrowsSpecific(block(), MyException); }
@interface MyException : NSException @end @implementation MyException @end
- (void)testNoThrowExceptionClass { /* 1 - // 2 - 3 - - */ void (^block)() = ^{ @throw [NSException exceptionWithName:NSGenericException reason:@"test throw" userInfo:nil]; }; XCTAssertNoThrowSpecific(block(), MyException); }
@interface MyException : NSException @end @implementation MyException @end
- (void)testThrowWithNamedExceptionClass { /* 1 - // 2 - 3 - 4 - - */ NSString *nameException = @"name expection"; void (^block)() = ^{ @throw [MyException exceptionWithName:nameException reason:@"test throw" userInfo:nil]; }; XCTAssertThrowsSpecificNamed(block(), MyException, nameException); }
@interface MyException : NSException @end @implementation MyException @end
- (void)testNoThrowWithNamedExceptionClass { /* 1 - // 2 - 3 - 4 - - */ NSString *nameException = @"name expection"; void (^block)() = ^{ @throw [MyException exceptionWithName:[nameException stringByAppendingString:@"123"] reason:@"test throw" userInfo:nil]; }; XCTAssertNoThrowSpecificNamed(block(), MyException, nameException); }
- (void)testEqualObject { /* 1 - isEqual 2 - isEqual 3 - - */ id obj1 = @[]; id obj2 = @[]; XCTAssertEqualObjects(obj1, obj2, @"obj1(%@) not equal to obj2(%@))", obj1, obj2); }
- (void)testNoEqualObject { /* 1 - isEqual 2 - isEqual 3 - - */ id obj1 = @"name"; id obj2 = @{}; XCTAssertNotEqualObjects(obj1, obj2, @"obj1(%@) not equal to obj2(%@))", obj1, obj2); }
- (void)testAsync { /* 1. ( ) 2. 3. 4. fulfill XCTestExpectation fulfill - */ XCTestExpectation *expectation = [self expectationWithDescription:@"block not call"]; NSTimeInterval timeout = 1.0f; [expectation performSelector:@selector(fulfill) withObject:nil afterDelay:0.3f]; [self waitForExpectationsWithTimeout:timeout handler:nil]; }
pod 'OCMock', '~> 3.1.2'
- (void)testInit { /* mock ClassB init ClassA , classA.classB , mockClassB */ id mockClassB = OCMClassMock([ClassB class]); ClassA *classA = [[ClassA alloc] initWithClassB:mockClassB]; XCTAssertEqual(classA.classB, mockClassB); }
- (void)testStub { /* mock ClassB stub , . ClassB */ NSString *expectedInfo = @"info"; id mockClassB = OCMClassMock([ClassB class]); OCMStub([mockClassB info]).andReturn(expectedInfo); NSString *info = [mockClassB info]; XCTAssertEqualObjects(info, expectedInfo); }
- (void)testStubWithArg { /* mock ClassB stub , */ id mockClassB = OCMClassMock([ClassB class]); NSInteger expectedFactorial3 = 6; NSInteger expectedFactorial5 = 120; OCMExpect([mockClassB factorial:3]).andReturn(expectedFactorial3); OCMExpect([mockClassB factorial:5]).andReturn(expectedFactorial5); NSInteger factorial3 = [mockClassB factorial:3]; NSInteger factorial5 = [mockClassB factorial:5]; XCTAssertEqual(factorial3, expectedFactorial3); XCTAssertEqual(factorial5, expectedFactorial5); }
- (void)testNotification { /* mock ClassB stub , mock observer mock mock observer */ id mockClassB = OCMClassMock([ClassB class]); NSString *notificationName = @"notification name"; NSNotification *notification = [NSNotification notificationWithName:notificationName object:mockClassB]; OCMStub([mockClassB postNotification]).andPost(notification); id mockObserver = OCMObserverMock(); [[NSNotificationCenter defaultCenter] addMockObserver:mockObserver name:notificationName object:mockClassB]; OCMExpect([mockObserver notificationWithName:notificationName object:mockClassB]); [mockClassB postNotification]; OCMVerifyAll(mockObserver); }
- (void)testVerifyExpect { /* mock ClassB info postNotification mock */ id mockClassB = OCMClassMock([ClassB class]); OCMExpect([mockClassB info]); OCMExpect([mockClassB postNotification]); [mockClassB info]; [mockClassB postNotification]; OCMVerifyAll(mockClassB); }
- (void)testPartialMockObject { /* ClassA, count. count readonly */ id classA = [[ClassA alloc] initWithCount:3]; XCTAssertEqual([classA count], 3); id partialMock = OCMPartialMock(classA); OCMStub([partialMock count]).andReturn(41); XCTAssertEqual([classA count], 41); }
- (void)testStubWithBlock { /* mock ClassB stub , mock ClassA , */ void (^block)() = [OCMArg checkWithBlock:^BOOL(id value) { return YES; }]; id mockClassB = OCMClassMock([ClassB class]); OCMExpect([mockClassB setBlock:block]); id mockClassA = OCMClassMock([ClassA class]); OCMStub([mockClassA useBlockInClassB]).andDo(^(NSInvocation *invocation) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [mockClassB setBlock:^{ }]; }); }); [mockClassA useBlockInClassB]; OCMVerifyAllWithDelay(mockClassB, 2); }
int main(int argc, char *argv[]) { @autoreleasepool { Class appDelegateClass = (NSClassFromString(@"XCTestCase") ? [TestingSAAppDelegate class] : [SAAppDelegate class]); return UIApplicationMain(argc, argv, nil, NSStringFromClass(appDelegateClass)); } }
Source: https://habr.com/ru/post/258953/
All Articles