Test Supplements iOS unit (a)

  Unit testing recently began to promote in the team, here is the use of Kiwi testing framework. You can refer to the detailed tutorial below the three articles
https://www.jianshu.com/p/ad55e197a8d3
https://onevcat.com/2014/02/ios-test-with-kiwi/
https://onevcat.com/ 2014/05 / kiwi-mock- stub-test /

  Unit testing not only to normal circumstances test for abnormal also need to be tested, the above three articles, on NSExceoption test, the above blog already has sample code that follows
to be tested Code:

- (double)pop {
    if ([self count] == 0) {
        [NSException raise:@"VVStackPopEmptyException" format:@"Can not pop an empty stack."];
    }
    double result = [self top];
    [self.numbers removeLastObject];
    return result;
}

Test code:

it(@"should raise a exception when pop", ^{
    [[theBlock(^{
        [stack pop];
    }) should] raiseWithName:@"VVStackPopEmptyException"];
});

If an exception occurs, we are able to successfully capture, then the test will pass. We often use in the project assert, to assert whether the unit tests to verify it, I tried it here is still possible, however, to note some points. Test code is as follows:
to be tested Code

- (void)setNewName
{
    NSAssert(NO, @"setNewName failed");
#warning todo
//    ......
}
[[[person setNewName] should] raiseWithName:@"setNewName"];

Note: The name of the method setNewNameto raiseWithName parameters and methods consistent.

If there are multiple assert a method, then we have to be differentiated according to reason.
Code to be tested as follows:

- (void)setNewName
{
    NSAssert(NO, @"setNewName failed");
#warning todo
//    ......
    NSAssert(NO, @"setNewName failed_1");

}

Test code is as follows:

[[[person setNewName] should] raiseWithName:@"setNewName" reason:@"setNewName failed"];
[[[person setNewName] should] raiseWithName:@"setNewName" reason:@"setNewName failed_1"];

Note: The assertion is reason desc content.
However, if a found function does not return a value in practice, the direct use of the following test samples is not enough

[[[person setNewName] should] raiseWithName:@"setNewName" reason:@"setNewName failed"];

At this time, the required functions in the block, something like this

it(@"should raise a exception when pop", ^{
    [[theBlock(^{
        [stack pop];
    }) should] raiseWithName:@"VVStackPopEmptyException"];
});

For easy to use, my side will function in unity within the block, to verify the assertion

More quality articles, you can focus on micro-channel scan code:
Write pictures described here

Published 231 original articles · won praise 110 · Views 600,000 +

Guess you like

Origin blog.csdn.net/HHL110120/article/details/103449273