• [Cocoa]OCMock 测试类方法


    OCMock 测试类方法

    本文遵循“署名-非商业用途-保持一致”创作公用协议

    使用 OCMock 进行 unit test 时,我们无法 stub 或 expect 类方法,那么又该怎样测试类方法呢?下面是一个解决办法:在测试类中的非类方法 wrap 一下类方法,然后测试 wrap 方法。

    比如:Utilities 有个类方法:

    + (NSString *) stringDate:(NSDate *)date withForamt:(NSString *)dateFormat;



    比如:我们在 UtilitiesTests 测试类中新建一个同样名称的测试函数,然后在该函数中转调 Utilities 的类方法,最后在 test 中对这个 wrap 函数进行测试:

    // stringDate:withForamt
    //
    - (NSString *) stringDate:(NSDate *)date withForamt:(NSString *)dateFormat
    {
    return [UIHUtilities stringDate:date withForamt:dateFormat];
    }

    - (void) testStringDateWithFormat
    {
    UIHUtilitiesTests *realObject = [[[UIHUtilitiesTests alloc] init] autorelease];
    id mock = [OCMockObject partialMockForObject:realObject];

    NSDate * date = [[NSDate alloc] initWithTimeIntervalSince1970:0];
    NSString * tgtFmt = @"yyyy/MM/dd";
    NSString * tgtValue = @"1970/01/01";

    NSString * returnValue = [mock stringDate:date withForamt:tgtFmt];
    GHAssertEqualStrings(tgtValue, returnValue, @"Should have returned the expected formatted date in string format.");
    }
  • 相关阅读:
    第五周任务以及结对编程
    “爆打”团队阿尔法发布 以及 第四周任务
    第三周内容
    爆打第一次站立会议
    3.23日PSP
    NABCD模型
    个人对结对编程的体会
    Fantacy团队第一次站立会议
    图形设想(简图)
    3月20日随笔
  • 原文地址:https://www.cnblogs.com/kesalin/p/OCMock_test_class_method.html
Copyright © 2020-2023  润新知