IOS测试之--断言

我只是代码界的搬运工(只为学习,方便大家)

一、Objective - C 中的断言:


Objective - C 中的断言处理使用的是 NSAssertionHandler :

每个线程拥有它自己的断言处理器,它是 NSAssertionHandler 类的实例对象。当被调用时,一个断言处理器打印一条包含方法和类名(或者函数名)的错误信息。然后它抛出一个 NSInternalInconsistencyException 异常。

基础类中定义了两套断言宏

1.NSAssert / NSCAssert

/** NSAssert */
#if !defined(_NSAssertBody)
#define NSAssert(condition, desc, ...)    \
do {                \
__PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
if (!(condition)) {        \
       NSString *__assert_file__ = [NSString stringWithUTF8String:__FILE__]; \
       __assert_file__ = __assert_file__ ? __assert_file__ : @"<Unknown File>"; \
   [[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd \
   object:self file:__assert_file__ \
       lineNumber:__LINE__ description:(desc), ##__VA_ARGS__]; \
}                \
   __PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
} while(0)
#endif

/** NSCAssert */
#if !defined(_NSCAssertBody)
#define NSCAssert(condition, desc, ...) \
do {                \
__PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
if (!(condition)) {        \
       NSString *__assert_fn__ = [NSString stringWithUTF8String:__PRETTY_FUNCTION__]; \
       __assert_fn__ = __assert_fn__ ? __assert_fn__ : @"<Unknown Function>"; \
       NSString *__assert_file__ = [NSString stringWithUTF8String:__FILE__]; \
       __assert_file__ = __assert_file__ ? __assert_file__ : @"<Unknown File>"; \
   [[NSAssertionHandler currentHandler] handleFailureInFunction:__assert_fn__ \
   file:__assert_file__ \
       lineNumber:__LINE__ description:(desc), ##__VA_ARGS__]; \
}                \
   __PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
} while(0)
#endif


2.NSParameterAssert / NSCParameterAssert
/** NSParameterAssert */
#define NSParameterAssert(condition) NSAssert((condition), @"Invalid parameter not satisfying: %@", @#condition)
/** NSCParameterAssert */
#define NSCParameterAssert(condition) NSCAssert((condition), @"Invalid parameter not satisfying: %@", @#condition)

这么做的意义在于两点:

  • 第一个是苹果对于断言处理在 API 层面进行了区分:
    • NSAssertNSCAssert 用来处理一般情况的断言
    • NSParameterAssertNSCParameterAssert 用来处理参数化的断言
  • 第二个是区别是在 Objective - C 和 C 之间进行了区分这样才有了:
    • NSAssertNSCAssert
    • NSParameterAssertNSCParameterAssert

  • 二、使用 NSAssertionHandler

    从 Xcode 4.2 开始,发布构建默认关闭了断言,它是通过定义 NS_BLOCK_ASSERTIONS 宏实现的。也就是说,当编译发布版时,任何调用 NSAssert 等的地方都被有效的移除了。
    NSAssertionHandler 还提供了一套优雅地处理断言失败的方式来保留珍贵的现实世界的使用信息。

    NSAssertionHandler 是一个很直接的类,带有两个需要在子类中实现的方法:
    -handleFailureInMethod:... (当 NSAssert / NSParameterAssert 失败时调用)
    和  -handleFailureInFunction:... (当 NSCAssert / NSCParameterAssert 失败时调用)。

    接下来看一个使用的实例
    #pragram 第一步,创建一个继承自NSAssertionHandler 的类:LoggingAssertionHandler 用来专门处理断言
    #import <Foundation/Foundation.h>
    @interface LoggingAssertionHandler : NSAssertionHandler
    @end
    #import "LoggingAssertionHandler.h"
    @implementation LoggingAssertionHandler
    /** 重写两个失败的回调方法,在这里执行我们想要抛出的错误(打印或者直接报错) */
    - (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format, ...{
      NSLog(@"NSAssert Failure: Method %@ for object %@ in %@#%li", NSStringFromSelector(selector), object, fileName, (long)line);
      NSException *e = [NSException
                        exceptionWithName: NSStringFromSelector(selector)
                        reason: format
                        userInfo: nil];
      @throw e;
    }
    - (void)handleFailureInFunction:(NSString *)functionName file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format, ...{
      NSLog(@"NSCAssert Failure: Function (%@) in %@#%li", functionName, fileName, (long)line);
    }
    @end


    每个线程都可以指定断言处理器。 

    想设置一个 NSAssertionHandler 的子类来处理失败的断言,在线程的threadDictionary 对象中设置 NSAssertionHandlerKey 字段即可。

    大部分情况下,你只需在

    -application:didFinishLaunchingWithOptions:

    中设置当前线程的断言处理器。

    AppDelegate 中的处理

    - (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    NSAssertionHandler *assertionHandler = [[LoggingAssertionHandler alloc] init];
    [[[NSThread currentThread] threadDictionary] setValue:assertionHandler
                                                   forKey:NSAssertionHandlerKey];
    // ...
    return YES;
    }
    这样我们就完成在当前线程中使用我们自定义的断言处理器的配置,那么接下来,如果有和我们条件不同的情况都直接会回调对应着的那两个失败的方法,我们可以在那俩个方法中按自己的输出意愿来处理你的话术。

    具体应用
    #import "ViewController.h"
    @interface ViewController ()
    @end
    @implementation ViewController
    - (void)viewDidLoad {
      [super viewDidLoad];
      NSObject*mc = [NSObject new];
      mc = @2;
      NSAssert(mc == nil, @"我不为空了");
    }
    @end
    根据输出情况可以看到是完全按照我们所需要的来输出的

    2015-10-30 21:33:14.529 NSAssert[20537:678428] *** 
    Terminating app due to uncaught exception 'viewDidLoad', reason: '我不为空了'


    三、使用上的注意点

    仔细观察 NSAssert 的宏定义 ,你会发现 self 的痕迹,有 self 的地方就一定要注意 block 容易产生的循环引用问题。
    /** NSAssert */
    #if !defined(_NSAssertBody)
    #define NSAssert(condition, desc, ...)    \
    do {                \
    __PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
    if (!(condition)) {        \
         NSString *__assert_file__ = [NSString stringWithUTF8String:__FILE__]; \
         __assert_file__ = __assert_file__ ? __assert_file__ : @"<Unknown File>"; \
     [[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd \
     object:self file:__assert_file__ \
         lineNumber:__LINE__ description:(desc), ##__VA_ARGS__]; \
    }                \
     __PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
    } while(0)
    #endif

    接下来举个例子:

    /** 创建一个 preson 类 */
    #import <Foundation/Foundation.h>
    typedef void(^mitchelBlock)(int num);
    @interface person : NSObject
    @property(nonatomic, copy)mitchelBlock block;
    @end
    #import "person.h"
    @implementation person
    - (instancetype)init{
      if (self = [super init]) {
          dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
              if (self.block) {
                  self.block(1);
              }
          });
      }
      return self;
    }
    @end
    /** ViewController 中的代码 */
    #import "ViewController.h"
    #import "person.h"
    @interface ViewController ()
    @property(nonatomic, strong)person * aPerson;
    @end
    @implementation ViewController
    - (void)viewDidLoad {
      [super viewDidLoad];
      NSObject*mc = [NSObject new];
      mc = @2;
      self.aPerson = [person new];
      self.aPerson.block = ^(int num){
          NSAssert(mc == nil, @"我不为空了"); 这里会有警告
          NSLog(@"%d",num);
      };
    }
    @end
    这样我们就会看到 Block 中循环引用的警告啦!

    那如果我想在 Block 中使用断言怎么办呐?用 NSCAssert 替换 NSAssertNSCParameterAssert 来替换 NSParameterAssert

    - (void)viewDidLoad {
      [super viewDidLoad];
      NSObject*mc = [NSObject new];
      mc = @2;
      self.aPerson = [person new];
      self.aPerson.block = ^(int num){
          NSCAssert(mc == nil, @"我不为空了");
          NSCParameterAssert(num>5);
      };
    }


    NSAssert使用例子及讲解

    NSAssert()只是一个宏,用于开发阶段调试程序中的Bug,通过为NSAssert()传递条件表达式来断定是否属于Bug,满足条件返回真值,程序继续运行,如果返回假值,则抛出异常,并切可以自定义异常描述。NSAssert()是这样定义的:

    #define NSAssert(condition, desc)

    condition是条件表达式,值为YES或NO;desc为异常描述,通常为NSString。当conditon为YES时程序继续运行,为NO时,则抛出带有desc描述的异常信息。NSAssert()可以出现在程序的任何一个位置。具体事例如下:

    生成一个LotteryEntry对象时,传入的NSDate不能为nil,加入NSAssert()判断。对象初始化源码如下:

    - (id)initWithEntryDate:(NSDate *)theDate {
        self = [super init];
        if (self) {
            NSAssert(theDate != nil, @"Argument must be non-nil");
            entryDate = theDate;
            firstNumber = (int)random() % 100 + 1;
            secondNumber = (int)random() % 100 + 1;
        }
        return  self;
    }

    接下来则是生成对象时传入一个值为nil的NSDate,看断言是否运行。

    LotteryEntry *nilEntry = [[LotteryEntry allocinitWithEntryDate:nil];

    断言效果如下:

    2013-01-17 20:49:12.486 lottery[3951:303] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Argument must be non-nil'
    *** First throw call stack:.......















    你可能感兴趣的:(断言,IOS测试,NSAssert,NSCAssert)