AOTestCase 1.0.3

AOTestCase 1.0.3

测试已测试
语言语言 Objective-CObjective C
许可证 MIT
发布最后发布2014年12月

Joshua Greene维护。



  • 作者:
  • Joshua Greene

AOTestCase 作为 的子类,并提供了异步测试支持,易于使用的方法交换以及一个用于设置和获取关联对象的类别。

手动安装

或者,您也可以手动将 AOTestCase.hAOTestCase.m 文件拖放到项目中,确保已选择了 Tests 目标(而不是主目标)。

如何使用

AOTestCase 旨在成为每个测试案例的基础测试案例类

1) 首先,创建一个新的测试案例,就像您通常做的那样(例如,从类模板创建)。

2) 在新测试案例的顶部导入 #import "AOTestCase.h"

3) 将 @interface TestCaseName : XCTestCase 替换为 @interface TestCaseName : AOTestCase,使您的测试案例继承自 AOTestCase 而不是 XCTestCase

方法交换

AOTestCase 使交换实例或类方法变得简单

1) 在您想要交换的类上创建一个新的类别

@interface AOTestObject (Test_Methods)
- (BOOL)swizzled_instanceMethod;
@end`

@implementation AOTestObject (Test_Methods)
- (BOOL)swizzled_instanceMethod
{
  return YES;
}
@end

2) 在您的测试方法中,使用 swapInstanceMethodsForClass...swapClassMethodsForClass... 交换实例或类方法

- (void)test___swapInstanceMethodsForClass
{
    // Swizzle the instance methods
    [self swapInstanceMethodsForClass:[AOTestObject class]
                           selector:@selector(instanceMethod)
                        andSelector:@selector(swizzled_instanceMethod)];

    // Call the original instance methods -> calls swizzled method
   XCTAssertTrue([testObject instanceMethod]);

   // Switch the methods back at the end of the test
  [self swapInstanceMethodsForClass:[AOTestCase class]
                           selector:@selector(swizzled_instanceMethod)
                        andSelector:@selector(instanceMethod)];
}

异步方法测试

AOTestCase 同样使得测试异步方法变得简单

- (void)test___waitForAsyncronousOperationToComplete
{
    // First call `beginAsynchronousOperation` before the operation begins
    [self beginAsynchronousOperation];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

        // Call `endAsynchronousOperation` when the operation ends
        [self endAsynchronousOperation];
    });

    // Call `waitForAsyncronousOperation` to wait on the operation
    // This returns `YES` if operation is ended successfully,
    // or `NO` if the operation timed out.
    BOOL success = [self waitForAsyncronousOperationWithTimeOut:1.0];
    XCTAssertTrue(success);
}

运行时对象关联

由于包含在 NSObject 中的 AOTestCase_Additions 类别,运行时关联对象同样很简单

要关联一个对象,只需调用 setAssociatedValue:key:,确保传入一个以引用方式传递的 const char 变量作为键

const char AOTestAssociationKey;
[testObject setAssociatedValue:@YES key:&AOTestAssociationKey];

通过调用 associatedValueForKey: 并传递相同的以引用方式传递的 const char 变量作为键,获取关联键的值同样简单

BOOL value = [testObject associatedValueForKey:&AOTestAssociationKey];
// value == YES

项目目标

主要目标

  • 尽可能简单地进行单元测试。

次要目标

  • 编写所有文档(所有头文件的 100% 文档)
  • 对所有代码行进行单元测试(所有代码行的 100% 单元测试)

贡献

欢迎提交错误修正和新功能!

为了做出贡献

1) 分支这个仓库 2) 修改您的代码 3) 添加单元测试(使用 XCTest 或 OCHamcrest 匹配器) 4) 添加文档(使用 appledoc 风格的行内注释) 5) 提交一个 puli 请求

如果您以前从未编写过单元测试,那完全没有关系!您可以通过查看 Jon Reid 的 (@qcoding) 精彩的 网站 学习,包括专门关于单元测试的 部分

由于该项目是 CocoaPods spec 仓库的一部分,其文档网页会在 CocoaDocs 上自动生成,由行内 appledoc 注释解析而来:CocoaDocs

如果您对 appledoc 不熟悉,请查看 Mattt Thompson 的 (@matt) 关于 appledoc 的介绍性 文章

许可证

AOTestCase 在 MIT 许可证下可用(有关更多详细信息,请参阅 LICENSE 文件)。