欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS尝试用测试驱动的方法开发一个列表模块【五】

程序员文章站 2022-04-30 20:13:30
ios尝试用测试驱动的方法开发一个列表模块【五】,第【四】篇的最后,我说道我碰到了一个令人纠结的代码重构的选择方案问题,到底选择让控制器成为可重用的控制器还是成为专用的控制器。让控制器可重用的重构方...

ios尝试用测试驱动的方法开发一个列表模块【五】,第【四】篇的最后,我说道我碰到了一个令人纠结的代码重构的选择方案问题,到底选择让控制器成为可重用的控制器还是成为专用的控制器。让控制器可重用的重构方案,会让代码具备更好的重用性、可变性和可测试性,我喜欢这种追求,我估摸着要做到这一点,工作量不会太大,所以我选择这种重构方案。

那么现在最主要的是重构cell的跳转部分的代码,我将把这部分代码从控制器里面剥离出来,放到独立的跳转类里面,然后让控制器通过协议依赖这个跳转类。我会让数据源代理类通过控制器跟跳转类协作,通过数据源代理类传递的数据决定让跳转类怎么执行跳转。跳转类引入了专有模块的数据model,要跳转的控制器,这两个类都不应该要求控制器知道,所以跳转类与数据源代理类相互间协作所使用到的公共方法都不涉及具体数据模型,数据源代理类像外界传的是id类型的数据,跳转类拿到id类型的数据后,自身判断它是不是自己所需的model,是的话就解析model做跳转,否则什么也不做。虽然如果让跳转类直接被数据源代理类所依赖的话,那么它们之间的交互可以使用模块专有的数据model,似乎很多事情会更方便,但是因为跳转类跟ui打交道,他需要知道要跳转的目的控制器和执行跳转的导航控制器,而数据源代理类,为了方便测试,并保持它的纯粹性,我希望它只做数据逻辑,不依赖ui相关的类(除了uitableviewcell),所以跳转类不会跟数据源类有直接关系,它将被控制器强引用,被控制器在数据源代理类响应表格cell点击事件后所使用。它被使用的公共方法将在它与控制器约定的协议里面定义。

有了想法后,我们继续用测试驱动的方式进行开发。

一,为控制器添加一个引用跳转类的属性。

【red:tc 5.1,控制器属性thejumper遵循jumperprotocol协议】
myviewcontrollertests.m文件:

/**
 tc 5.1
 */
- (void)test_property_thejumper_conformjumperprotocol{
    nsstring *typename = [nsobject typeforproperty:@"thejumper" inclass:@"myviewcontroller"];
    xctasserttrue([typename isequaltostring:@""]);
}

【green,定义jumperprotocol,往控制器添加thejumper属性,让测试通过】
myviewcontroller.h文件:

#import 
#import "mydatasourceprotocol.h"
#import "jumperprotocol.h"

@interface myviewcontroller : uiviewcontroller

@property (nonatomic, strong) uitableview *thetableview;
@property (nonatomic, strong) id thedatasource;
@property (nonatomic, strong) id thejumper;

@end

jumperprotocol.h文件:

#import 

/**
 跳转类与控制器约定的协议
 */
@protocol jumperprotocol 

@end

【red:tc 5.2,控制器thejumper属性是强引用】

/**
 tc 5.2
 */
- (void)test_property_thejumper_isstronglyrefered{
    @autoreleasepool {
        self.thecontroller.thejumper = (id)[[nsobject alloc] init];
    }
    // weak引用,会被自动释放池释放,强引用不会。
    xctassertnotnil(self.thecontroller.thejumper);
}

【green,当前定义的属性已经满足此测试用例】

二,实现一个专门的cell点击跳转类。

【red,tc 5.3 跳转类要实现jumperprotocol协议】
新建一个关于跳转类的测试类,添加这个测试用例。
myjumpertests.m文件:

/**
 tc 5.3
 */
- (void)test_shouldconformjumperprotocol{
    mycelljumper *jumper = [[mycelljumper alloc] init];
    xctasserttrue([jumper conformstoprotocol:@protocol(jumperprotocol)]);
}

【green,创建mycelljumper类并让它遵循jumperprotocol协议,让上面测试用例通过】
mycelljumper.h文件:

#import "jumperprotocol.h"

@interface mycelljumper : nsobject 

@end

【red:tc 5.4 跳转类要实现一个接受一个id类型的参数的跳转方法】
myjumpertests.m文件:

/**
 tc 5.4
 */
- (void)test_method_tocontrollerwithdata_shouldbeimplemented{
    mycelljumper *jumper = [[mycelljumper alloc] init];
    xctasserttrue([jumper respondstoselector:@selector(tocontrollerwithdata:)]);
}

【green,给jumperprotocol协议添加方法- (void)
tocontrollerwithdata:(id)data,并让mycelljumper实现它,让上面测试用例通过】
jumperprotocol.h文件:

/**
 各个模块的jumper实现这个方法时,要在方法里面对data做判断,data是想要的数据时,才解析
 拿出数据,执行跳转。

 @param data <#data description#>
 */
- (void)tocontrollerwithdata:(id)data;

mycelljumper.m文件:

#import "mycelljumper.h"

@implementation mycelljumper

#pragma mark - jumperprotocol

- (void)tocontrollerwithdata:(id)data{

}

@end

【red:tc 5.5,mycelljumper应该实现一个依赖于导航控制器的初始化方法】
myjumpertests.m文件:

/**
 tc 5.5
 */
- (void)test_method_initwithnavigationcontroller_shouldbeimplemented{
    mycelljumper *jumper = [[mycelljumper alloc] init];
    xctasserttrue([jumper respondstoselector:@selector(initwithnavigationcontroller:)]);
}

因为跳转类一定要用到导航控制器,所以吧这个初始化方法作为协议必须实现的方法。
【green:往jumperprotocol里面添加- (instancetype)initwithnavigationcontroller:(uinavigationcontroller *)navvc方法,并让mycelljumper.m实现它】
jumperprotocol.h文件:

/**
 这是应该被使用的正确的初始化方法。
 1,navvc不能为空。
 2,navvc应该在内部被弱引用。

 @param navvc <#navvc description#>
 @return <#return value description#>
 */
- (instancetype)initwithnavigationcontroller:(uinavigationcontroller *)navvc;

mycelljumper.m文件:

- (instancetype)initwithnavigationcontroller:(uinavigationcontroller *)navvc{
    return nil;
}

现在一般跳转类所需的公共方法已经设计完成,接下来看怎么实现mycelljumper这个跳转类的这些方法,来保证它能够被正确初始化和实现正确的跳转。
【red:tc 5.6,mycelljumper的初始化方法不能传入空的导航控制器,否则会触发断言异常】
myjumpertests.m文件:

/**
 tc 5.6
 */
- (void)test_shouldnotpassnilwheninitwithnavigationcontroller{
    xctassertthrows([[mycelljumper alloc] initwithnavigationcontroller:nil]);
}

【green,在mycelljumper的初始化方法里面加入判断导航控制器是否存在的断言】
mycelljumper.m文件:

- (instancetype)initwithnavigationcontroller:(uinavigationcontroller *)navvc{
    nsassert(navvc, @"导航控制器不能为nil");
    return nil;
}

【red:tc 5.7,mycelljumper对导航控制器的持有应该是弱引用】
mycelljumpertests.m文件:

/**
 tc 5.7
 */
- (void)test_navigationcontroller_shouldbeweaklyrefered{
    __block mycelljumper *jumper;
    @autoreleasepool {
        uinavigationcontroller *navvc = [[uinavigationcontroller alloc] init];
        jumper = [[mycelljumper alloc] initwithnavigationcontroller:navvc];
    }
    xctassertnil(jumper.navigationcontroller);
}

这里碰到了有趣的事情,为了写上面的测试用例,需要mycelljumper暴露一个导航控制器的引用属性,这个属性本可以不暴露的,但是,我们为了增强类的可测试性,把它暴露出来了,这种暴露与不暴露是需要平衡的,毕竟有些情况,暴露的东西多了,就破坏了类的封装性了,而什么都不暴露,类就没有很好的可测试性,不利于我们做单元测试。这里可以看出测试驱动开发的一个好处,即在开发过程中促使我们去考虑如何让代码为测试提供方便。毕竟,若我们不考虑测试性,那么我们的测试用例便写不下去了。针对mycelljumper这个类,我认为暴露一个只读的指向导航控制器的属性是可以的,我们不用担心它会被无意地修改,也满足了我们的测试需求。
【green:在mycelljumper类的初始化方法里面,把传入的导航控制器付给它的navigationcontroller属性,这个属性是weak, readonly修饰的】
mycelljumper.h文件:

#import "jumperprotocol.h"

@interface mycelljumper : nsobject 

@property (nonatomic, readonly, weak) uinavigationcontroller *navigationcontroller;

@end

mycelljumper.m文件:

- (instancetype)initwithnavigationcontroller:(uinavigationcontroller *)navvc{
    nsassert(navvc, @"导航控制器不能为nil");
    _navigationcontroller = navvc;
    return nil;
}

满足了【tc 5.6,tc 5.7】的初始化方法还不能用,再添加一个测试用例让它变成真正的初始化方法
【red:tc 5.8,mycelljumper类的初始化方法要返回一个mycelljumper对象,对象的navigationcontroller属性应该引用一个导航控制器对象】
myjumpertests.m文件:

/**
 tc 5.8
 */
- (void)test_initmethod_shouldreturnaselftypeinstance_and_property_navigationcontroller_shouldreferanavigationcontrollerinstanceafterinit{
    uinavigationcontroller *navvc = [[uinavigationcontroller alloc] init];
    id obj = [[mycelljumper alloc] initwithnavigationcontroller:navvc];
    xctasserttrue([obj iskindofclass:[mycelljumper class]]);
    mycelljumper *jumper = obj;
    xctasserttrue([jumper.navigationcontroller iskindofclass:[uinavigationcontroller class]]);
}

【green,修改mycelljumper的初始化方法】
mycelljumper.m文件:

- (instancetype)initwithnavigationcontroller:(uinavigationcontroller *)navvc{
    nsassert(navvc, @"导航控制器不能为nil");
    if (self = [super init]) {
        _navigationcontroller = navvc;
    }

    return self;
}

对mycelljumper的初始化方法,我们已经用测试用例覆盖得差不多了,在接下去开发跳转方法之前,先对测试代码执行一个refactor流程,因为发现了大部分myjumpertests.m里面的测试用例的新建一个mycelljumper对象的代码可以重用,因此把这部分代码提取到setup方法去执行。
【refactor:提取各个测试用例的可重用代码到setup方法,用类成员变量self.jumper对象代替一些测试用例里面的局部变量jumper对象】
myjumpertests.m文件:

@interface myjumpertests : xctestcase

@property (nonatomic, strong) uinavigationcontroller *navvc;
@property (nonatomic, strong) mycelljumper *jumper;

@end

@implementation myjumpertests

- (void)setup {
    [super setup];
    self.navvc = [[uinavigationcontroller alloc] init];
    self.jumper = [[mycelljumper alloc] initwithnavigationcontroller:self.navvc];
}

- (void)teardown {
    self.navvc = nil;
    self.jumper = nil;
    [super teardown];
}

重构后,重新运行所有myjumpertests.m里面的测试用例,仍然全部通过,说明重构没问题。

接下来将针对跳转类真正处理跳转逻辑的核心方法做测试驱动开发。将跳转逻辑封装进独立的类来处理,将让这部分逻辑变得非常有利于做单元测试。通过给这个类传参,再观察它能否对可能情形的参数做出正确的处理,产生正确的目的控制器对象,并执行了导航控制器的push方法,我们就能用单元测试用例充分覆盖到所有需要测试的逻辑。

现在继续往myjumpertests.m里面添加测试用例,并继续修改mycelljumper类让这些测试用例通过。

首先测试跳转方法对异常情况的处理,当传入nil和非mymodel类型的参数时它不执行任跳转。

如何验证不执行跳转?其实就是验证导航控制器没有调用push方法。所以这里要用到文章【四】里面创建的fakenavigationviewcontroller来代替真实的导航控制器来跟跳转类交互,因为唯有在假导航控制器对象里面,我们经过了可测试处理后,才能感知push方法是否执行了。其实这里也不能说它是假对象,比较它是真导航控制器的子类,能执行真正的push方法,更准确的说法是它是一个可测试性的导航控制器对象。无论是用假对象,或可测试对象来替换产品代码里面原有的对象,都是用了同样的测试技术,将被测对象与其依赖的对象隔离开来,用我们设计好的假对象来替换这些依赖的对象,然后我们就可以通过感知到假对象与被测对象交互过程中发生的变化来测试被测对象的外资行为。至于怎么创建假对象来实现这种隔离,一般有通过接口隔离,通过子类替换,通过方法替换等技术方法。这里使用的就是通过子类替换的方法。

【tc 5.9,保证跳转方法传nil时不跳转】
【tc 5.10,保证跳转方法传非mymodel类型参数时不跳转】
myjumpertests.m文件:

/**
 tc 5.9
 */
- (void)test_method_tocontrollerwithdata_donotpushwithnil{
    __block nsstring *calledmethod;
    fakenavigationviewcontroller *nav = [[fakenavigationviewcontroller alloc] init];
    nav.callmethodblock = ^(nsstring *methodname, nsdictionary *parameters) {
        calledmethod = methodname;
    };
    mycelljumper *jumper = [[mycelljumper alloc] initwithnavigationcontroller:nav];
    [jumper tocontrollerwithdata:nil];
    xctassertnil(calledmethod);
}

/**
 tc 5.10
 */
- (void)test_method_tocontrollerwithdata_donotpushwithnotmymodeltypedata{
    __block nsstring *calledmethod;
    fakenavigationviewcontroller *nav = [[fakenavigationviewcontroller alloc] init];
    nav.callmethodblock = ^(nsstring *methodname, nsdictionary *parameters) {
        calledmethod = methodname;
    };
    mycelljumper *jumper = [[mycelljumper alloc] initwithnavigationcontroller:nav];
    nsobject *otherpara = [[nsobject alloc] init];
    [jumper tocontrollerwithdata:otherpara];
    xctassertnil(calledmethod);
}

不需要对现有mycelljumper代码做任何改动,这两个测试用例也会通过,因为跳转方法还什么都没做呢。

然后测试跳转方法在传入mymodel类型参数时能否实现正确的跳转。
【red:tc 5.11,测试当model数据类型为a类型时,要跳转到a类型指定控制器】
myjumpertests.m文件:

/**
 tc 5.11
 */
- (void)test_jumptoatypeviewcontroller_withatypedata{
    __block nsstring *calledmethod;
    __block uiviewcontroller *controller;
    fakenavigationviewcontroller *nav = [[fakenavigationviewcontroller alloc] init];
    nav.callmethodblock = ^(nsstring *methodname, nsdictionary *parameters) {
        calledmethod = methodname;
        controller = parameters[[fakenavigationviewcontroller pushcontrollerparakey]];
    };
    mycelljumper *jumper = [[mycelljumper alloc] initwithnavigationcontroller:nav];
    nsobject *otherpara = [[nsobject alloc] init];
    [jumper tocontrollerwithdata:otherpara];
    xctasserttrue([calledmethod isequaltostring:[fakenavigationviewcontroller pushmethodname]]);
    xctasserttrue([controller iskindofclass:[atypeviewcontroller class]]);
}

【green:往跳转方法里面实现对a类型数据的跳转逻辑】
mycelljumper.m文件:

- (void)tocontrollerwithdata:(id)data{
    mymodel *model = data;
    if (model.type == modeltypea) {
        atypeviewcontroller *vc = [[atypeviewcontroller alloc] init];
        [self.navigationcontroller pushviewcontroller:vc animated:yes];
    }
}

运行myjumpertests.m里面所有测试用例,【tc 5.11】通过了,但是【tc 5.9,tc 5.10】失败了。

iOS尝试用测试驱动的方法开发一个列表模块【五】

虽然前面【tc 5.9,tc 5.10】一开始不用做任何代码修改它们就通过了,感觉没什么用处,但此刻它们起到了捕获bugs的作用,它们分别揭示了当前跳转方法的实现的两个问题:1,传参为nil时也能满足model.type == modeltypea的条件;2,传参为非nil非mymodel类型数据时将会因为unrecognized selector问题发生崩溃。
我们继续完善跳转方法的实现,修复着两个bugs。
mycelljumper.m文件:

- (void)tocontrollerwithdata:(id)data{
    if (!data || ![data iskindofclass:[mymodel class]]) {
        return;
    }
    mymodel *model = data;
    if (model.type == modeltypea) {
        atypeviewcontroller *vc = [[atypeviewcontroller alloc] init];
        [self.navigationcontroller pushviewcontroller:vc animated:yes];
    }
}

终于,现在我们让所有测试用例都green了,这感觉真棒!
接下来还有对b类型、c类型数据的跳转的测试用例需要添加,不过在进一步测试之前,我们又发现了这是可以进行一次refactor流程的好时机,因为【tc 5.9,tc 5.10,tc 5.11】之间有不少冗余代码可以清理。
【refactor:清理测试用例冗余代码,让它们更简洁】
将冗余代码放入setup文件。
myjumpertests.m文件:

@interface myjumpertests : xctestcase

@property (nonatomic, strong) uinavigationcontroller *navvc;
@property (nonatomic, strong) fakenavigationviewcontroller *fakenavvc;
@property (nonatomic, strong) mycelljumper *jumper;
@property (nonatomic, strong) mycelljumper *jumperwithfakenavvc;
@property (nonatomic, strong) uiviewcontroller *pushedcontroller;
@property (nonatomic, strong) nsstring *pushmethod;

@end

@implementation myjumpertests

- (void)setup {
    [super setup];
    // 依赖于可测试导航栏控制器的jumper
    self.fakenavvc = [[fakenavigationviewcontroller alloc] init];
    __weak typeof(self) wself = self;
    self.fakenavvc.callmethodblock = ^(nsstring *methodname, nsdictionary *parameters) {
        __strong typeof(self) sself = wself;
        sself.pushmethod = methodname;
        sself.pushedcontroller = parameters[[fakenavigationviewcontroller pushcontrollerparakey]];
    };
    self.jumperwithfakenavvc = [[mycelljumper alloc] initwithnavigationcontroller:self.fakenavvc];
    // 正常的jumper
    self.navvc = [[uinavigationcontroller alloc] init];
    self.jumper = [[mycelljumper alloc] initwithnavigationcontroller:self.navvc];
}

- (void)teardown {
    self.navvc = nil;
    self.jumper = nil;
    self.pushedcontroller = nil;
    self.pushmethod = nil;
    [super teardown];
}

【tc 5.9,tc 5.10,tc 5.11】由原来的一长串代码变成很少的几行代码,而且可以预期,接下来新增的两个数据类型跳转的测试用例也仍然是几行代码。
myjumpertests.m文件:

/**
 tc 5.9
 */
- (void)test_method_tocontrollerwithdata_donotpushwithnil{
    [self.jumperwithfakenavvc tocontrollerwithdata:nil];
    xctassertnil(self.pushmethod);
}

/**
 tc 5.10
 */
- (void)test_method_tocontrollerwithdata_donotpushwithnotmymodeltypedata{
    nsobject *otherpara = [[nsobject alloc] init];
    [self.jumperwithfakenavvc tocontrollerwithdata:otherpara];
    xctassertnil(self.pushmethod);
}

/**
 tc 5.11
 */
- (void)test_jumptoatypeviewcontroller_withatypedata{
    mymodel *model = [[mymodel alloc] init];
    model.type = modeltypea;
    [self.jumperwithfakenavvc tocontrollerwithdata:model];
    xctasserttrue([self.pushmethod isequaltostring:[fakenavigationviewcontroller pushmethodname]]);
    xctasserttrue([self.pushedcontroller iskindofclass:[atypeviewcontroller class]]);
}

这次refactor效果不错,在很好地减少了测试代码的同时,让测试用例的测试意图表达得更简洁直观了。

现在开始添加对b、c类型,和其他类型的数据的跳转逻辑,完成我们的跳转方法的测试开发。
【red:tc 5.12,测试保证b类型数据跳转到b类型指定控制器】
myjumpertests.m文件:


/**
 tc 5.12
 */
- (void)test_jumptobtypeviewcontroller_withbtypedata{
    mymodel *model = [[mymodel alloc] init];
    model.type = modeltypeb;
    [self.jumperwithfakenavvc tocontrollerwithdata:model];
    xctasserttrue([self.pushmethod isequaltostring:[fakenavigationviewcontroller pushmethodname]]);
    xctasserttrue([self.pushedcontroller iskindofclass:[btypeviewcontroller class]]);
}

/**
 tc 5.13
 */
- (void)test_jumptoctypeviewcontroller_withctypedata{
    mymodel *model = [[mymodel alloc] init];
    model.type = modeltypec;
    [self.jumperwithfakenavvc tocontrollerwithdata:model];
    xctasserttrue([self.pushmethod isequaltostring:[fakenavigationviewcontroller pushmethodname]]);
    xctasserttrue([self.pushedcontroller iskindofclass:[ctypeviewcontroller class]]);
}

/**
 tc 5.14
 */
- (void)test_donotpushwhenmymodeltypedatawithothertypevalue{
    mymodel *model = [[mymodel alloc] init];
    model.type = 100;
    [self.jumperwithfakenavvc tocontrollerwithdata:model];
    xctassertnil(self.pushmethod);
    xctassertnil(self.pushedcontroller);
}

【green:新建btypeviewcontroller、ctypeviewcontroller类,修改跳转方法实现】
mycelljumper.m文件:

- (void)tocontrollerwithdata:(id)data{
    if (!data || ![data iskindofclass:[mymodel class]]) {
        return;
    }
    mymodel *model = data;
    uiviewcontroller *vc;
    switch (model.type) {
        case modeltypea:
            vc = [[atypeviewcontroller alloc] init];
            break;
        case modeltypeb:
            vc = [[btypeviewcontroller alloc] init];
            break;
        case modeltypec:
            vc = [[ctypeviewcontroller alloc] init];
            break;
        default:{
            return;
        }
            break;
    }
    [self.navigationcontroller pushviewcontroller:vc animated:yes];
}

至此,跳转方法已经测试开发完成,同时,这个cell的专门跳转类也已经开发测试完成,下一步,就是要在控制器里面使用它,看能不能达到我们将控制器与跳转逻辑解耦的目的。

三,用跳转类在控制器里面实现cell的跳转逻辑。

首先要修改数据源代理类mytableviewdatasource的celltapblock,让它传递一个id类型的参数用来给跳转类mycelljumper接收。原来有一个相关的测试用例【tc 4.6】,它当前测试的是cell被tapped后是否将cell的row通过celltapblock传递了出去,我们现在修改让它传递cell的数据模型。

【red:tc 4.6,修改为表格数据源代理类在cell被点击时应该要将cell对应的数据model传递给外界】
这是对数据源代理类的修改,所以测试用例放在它对应的测试类里面。
mytableviewdatasourcetests.m文件:

/**
 tc 4.6
 */
- (void)test_celltapblockreceivedataoftappedcell{
    self.datasource.thedataarray = @[@{@"type":@0,@"title":@"type a title",@"someid":@"0001"},@{@"type":@1,@"title":@"type b title",@"someid":@"0002"}];
    __block id model;
    self.datasource.celltapblock = ^(id datamodel){
        model = datamodel;
    };
    nsindexpath *indexpath = [nsindexpath indexpathforrow:1 insection:0];
    [self.datasource tableview:self.thetableview didselectrowatindexpath:indexpath];
    xctassertnotnil(model);
    xctasserttrue([model iskindofclass:[mymodel class]]);
    mymodel *cellmodel = model;
    xctasserttrue([cellmodel.someid isequaltostring:@"0002"]);
    xctasserttrue([cellmodel.title isequaltostring:@"type b title"]);
    xctasserttrue(cellmodel.type == modeltypeb);
}

显而易见的,我发现另一个测试用例【tc 4.5】也得做响应的修改,把celltapblock的参数改为id类型。
mytableviewdatasourcetests.m文件:

/**
 tc 4.5
 */
- (void)test_executecelltapblockifcellselectedmethodcalled{
    __block bool called = no;
    self.datasource.celltapblock = ^(id datamodel){
        called = yes;
    };
    nsindexpath *indexpath = [nsindexpath indexpathforrow:0 insection:0];
    [self.datasource tableview:self.thetableview didselectrowatindexpath:indexpath];
    xctasserttrue(called);
}

做完这些改动,我这次没有全部运行一次所有测试用例,我就先去改产品代码了。
【green:修改数据源代理类与控制器之间的交互协议,修改数据源代理类的cell选择代理方法的实现】
在mydatasourceprotocol.h文件里面修改celltapblock的参数:

@protocol mydatasourceprotocol 

@optional
@property (nonatomic, strong) nsarray *thedataarray;
@property (nonatomic, copy) void(^updateblock)();
@property (nonatomic, copy) void(^celltapblock)(id datamodel);

@end

在mytableviewdatasource.m文件里面修改cell选择的代理方法的实现:

- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath{
    if (self.celltapblock) {
        mymodel *model = [[mymodel alloc] init];
        nsdictionary *data = self.thedataarray[indexpath.row];
        model.someid = data[@"someid"];
        model.title = data[@"title"];
        model.type = [data[@"type"] integervalue];
        self.celltapblock(model);
    }
}

然后,执行一次整个工程的所有测试用例,以上的两个测试用例重新通过了,但是发现了控制器的测试用例里面有一个失败了。
iOS尝试用测试驱动的方法开发一个列表模块【五】
反正我们也要开始改控制器的代码了,就把这个测试用例作为我们这次对控制器修改的第一个red流程吧。只不过,它的命名和实现都要修改一番,以表达我们新的测试意图。原来我们的做法是通过让控制器调用数据源代理类的cell选择代理方法,然后检测控制器的导航控制器属性(通过用fake替换真实的导航栏控制器的方法来感知)是否拿到了正确的要被pushed的控制器对象,是否执行了push方法,来验证cell的选择事件是否导致了正确的push行为。现在我们不用这么做了,因为,我们已经在myjumpertests.m里面对跳转类的跳转逻辑进行了单元测试覆盖,而且都测试通过,说明了myjumper类是可靠的,在控制器这边的跳转逻辑测试,我们只需要测试在cell被选择后,控制器的thejumper对象是否执行了跳转方法,以及它的跳转方法是否拿到了正确的参数即可。那么如何感知thejumper对象是否执行了方法,拿到了正确的参数?我们是不是继续像前面做法一样通过fakenavigationviewcontroller来感知它是否执行了push和拿到了要push的控制器对象?不,因为现在我们要测的是控制器,跳转逻辑部分是myjumper类对象直接与控制器打交道,导航控制器对象如何被操作属于myjumper的实现细节了,它离我们的测试目标比较远,我们不应该让它做感知对象。而应该找一个对象,替换直接与控制器打交道的myjumper类对象,来作为对控制器行为的感知对象。只要想到要通过替换对象的方式来做测试,通常就想到要创建一个假对象或者一个可测试的对象,这种对象的创建方法要么是通过创建子类,要么是通过实现协议,这里因为thejumper属性与控制器之间是通过协议来交互的,所以,我们创建一个跟thejumper属性实现同样协议的对象来替换真实的myjumper类对象,作为控制器的thejumper属性,然后在测试用例里面使用它来感知控制器是否正确地使用了它。
【red:tc 4.8,修改控制器的这个测试用例为在数据源代理类对象在响应了cell的选择代理方法后,跳转类对象是否调用了跳转方法】
myviewcontrollertests.m文件:

/**
 tc 4.8
 */
- (void)test_selectacellthejumpercalljumpmethod{
    uinavigationcontroller *navvc = [[uinavigationcontroller alloc] init];
    fakemyjumper *jumper = [[fakemyjumper alloc] initwithnavigationcontroller:navvc];
    __block nsstring *name;
    jumper.callmethodblock = ^(nsstring *methodname, nsdictionary *parameters) {
        name = methodname;
    };
    self.thecontroller.thejumper = jumper;
    self.thecontroller.thedatasource = self.thedatasource;
    [self.thecontroller viewdidload];
    [self.thedatasource tableview:self.thetableview didselectrowatindexpath:[nsindexpath indexpathforrow:0 insection:0]];
    xctasserttrue([name isequaltostring:[fakemyjumper jumpmethodname]]);
}

【green:创建fakemyjumper类,修改控制器viewdidload里面处理跳转的逻辑】
fakemyjumper要替换myjumper类,就得实现跟它一样实现jumperprotocol协议;同时要具备可测试性就得导入nsobject+testinghelper类别,在要检测的方法里面,执行callmethodblock。
fakemyjumper.h文件:

#import "jumperprotocol.h"
#import "nsobject+testinghelper.h"

@interface fakemyjumper : nsobject 

/**
 获取常亮的方法名字符串,方便用来做检测对比。

 @return <#return value description#>
 */
+ (nsstring *)jumpmethodname;

@end

fakemyjumper.m文件:

#import "fakemyjumper.h"

@implementation fakemyjumper

#pragma mark - jumperprotocol

- (void)tocontrollerwithdata:(id)data{
    // 调用了本方法,外界就能检测到
    if (self.callmethodblock) {
        self.callmethodblock([fakemyjumper jumpmethodname], nil);
    }
}

- (instancetype)initwithnavigationcontroller:(uinavigationcontroller *)navvc{
    return self;
}

+ (nsstring *)jumpmethodname{
    return @"tocontrollerwithdata:";
}

@end

控制器的viewdidload方法里面处理跳转的逻辑将由原来的这种硬编码:
myviewcontroller.m文件:

- (void)viewdidload {
    //  其他代码。。。
    //  cell跳转逻辑
    self.thedatasource.celltapblock = ^(nsindexpath *indexpath) {
        __strong typeof(self) sself = wself;
        nsdictionary *data = sself.thedatasource.thedataarray[indexpath.row];
        if ([data[@"type"] integervalue] == 0) {
            atypeviewcontroller *vc = [[atypeviewcontroller alloc] init];
            [sself.navigationcontroller pushviewcontroller:vc animated:yes];
        }
    };
    //  其他代码。。。
}

变成这样的依赖协议的简洁代码:
myviewcontroller.m文件:

- (void)viewdidload {
    //  其他代码。。。
    //  cell跳转逻辑
    self.thedatasource.celltapblock = ^(id datemodel) {
        __strong typeof(self) sself = wself;
        if (sself.thejumper) {
            [sself.thejumper tocontrollerwithdata:datemodel];
        }
    };
    //  其他代码。。。
}

【red:tc 5.15,当选择a类型的cell时,跳转类对象获得a类型的model数据】
myviewcontrollertests.m文件:

/**
 tc 5.15
 */
- (void)test_selectatypecellpassatypemodeltothejumper{
    uinavigationcontroller *navvc = [[uinavigationcontroller alloc] init];
    fakemyjumper *jumper = [[fakemyjumper alloc] initwithnavigationcontroller:navvc];
    __block nsstring *name;
    __block id model;
    jumper.callmethodblock = ^(nsstring *methodname, nsdictionary *parameters) {
        name = methodname;
        model = parameters[[fakemyjumper modelkey]];
    };
    self.thecontroller.thejumper = jumper;
    self.thedatasource.thedataarray = @[@{@"type":@0,@"title":@"type a title",@"someid":@"0001"},@{@"type":@1,@"title":@"type b title",@"someid":@"0002"}];
    self.thecontroller.thedatasource = self.thedatasource;
    [self.thecontroller viewdidload];
    [self.thedatasource tableview:self.thetableview didselectrowatindexpath:[nsindexpath indexpathforrow:0 insection:0]];
    xctasserttrue([name isequaltostring:[fakemyjumper jumpmethodname]]);
    xctasserttrue([model iskindofclass:[mymodel class]]);
    mymodel *datamodel = model;
    xctasserttrue(datamodel.type == modeltypea);
}

【green:修改fakemyjumper,让它可以将接收的参数通过block传递出来;修改控制器,去掉#import "atypeviewcontroller.h"】
fakemyjumper.h文件,添加获取参数字典key的方法:

/**
 从参数字典里面获取model对象的key

 @return <#return value description#>
 */
+ (nsstring *)modelkey;

fakemyjumper.m文件,调用方法时,把参数传入block:

- (void)tocontrollerwithdata:(id)data{
    // 调用了本方法,外界就能检测到
    if (self.callmethodblock) {
        self.callmethodblock([fakemyjumper jumpmethodname], @{[fakemyjumper modelkey]:data});
    }
}
+ (nsstring *)modelkey{
    return @"datamodel";
}

重新运行所有测试,全部通过。继续添加对b, c类型cell的跳转测试。
【red:tc 5.16,当选择b类型的cell时,跳转类对象获得b类型的model数据】
myviewcontrollertests.m文件:

/**
 tc 5.16
 */
- (void)test_selectbtypecellpassbtypemodeltothejumper{
    uinavigationcontroller *navvc = [[uinavigationcontroller alloc] init];
    fakemyjumper *jumper = [[fakemyjumper alloc] initwithnavigationcontroller:navvc];
    __block nsstring *name;
    __block id model;
    jumper.callmethodblock = ^(nsstring *methodname, nsdictionary *parameters) {
        name = methodname;
        model = parameters[[fakemyjumper modelkey]];
    };
    self.thecontroller.thejumper = jumper;
    self.thedatasource.thedataarray = @[@{@"type":@0,@"title":@"type a title",@"someid":@"0001"},@{@"type":@1,@"title":@"type b title",@"someid":@"0002"}];
    self.thecontroller.thedatasource = self.thedatasource;
    [self.thecontroller viewdidload];
    [self.thedatasource tableview:self.thetableview didselectrowatindexpath:[nsindexpath indexpathforrow:1 insection:0]];
    xctasserttrue([name isequaltostring:[fakemyjumper jumpmethodname]]);
    xctasserttrue([model iskindofclass:[mymodel class]]);
    mymodel *datamodel = model;
    xctasserttrue(datamodel.type == modeltypeb);
}

/**
 tc 5.17
 */
- (void)test_selectctypecellpassctypemodeltothejumper{
    uinavigationcontroller *navvc = [[uinavigationcontroller alloc] init];
    fakemyjumper *jumper = [[fakemyjumper alloc] initwithnavigationcontroller:navvc];
    __block nsstring *name;
    __block id model;
    jumper.callmethodblock = ^(nsstring *methodname, nsdictionary *parameters) {
        name = methodname;
        model = parameters[[fakemyjumper modelkey]];
    };
    self.thecontroller.thejumper = jumper;
    self.thedatasource.thedataarray = @[@{@"type":@0,@"title":@"type a title",@"someid":@"0001"},@{@"type":@1,@"title":@"type b title",@"someid":@"0002"},@{@"type":@2,@"title":@"type c title",@"someid":@"0003"}];
    self.thecontroller.thedatasource = self.thedatasource;
    [self.thecontroller viewdidload];
    [self.thedatasource tableview:self.thetableview didselectrowatindexpath:[nsindexpath indexpathforrow:2 insection:0]];
    xctasserttrue([name isequaltostring:[fakemyjumper jumpmethodname]]);
    xctasserttrue([model iskindofclass:[mymodel class]]);
    mymodel *datamodel = model;
    xctasserttrue(datamodel.type == modeltypec);
}

【green:不用修改任何产品代码,这两个测试用例就通过了】

留意到【tc 5.15,tc 5.16,tc 5.17】有很多冗余的代码,所以又到了可以执行refactor流程的点。
【refactor:整理测试代码,消除冗余】
提取公共方法,将测试用例常用的变量作为测试类的成员变量。
myviewcontrollertests.m文件:

@interface myviewcontrollertests : xctestcase

@property (nonatomic, strong) uitableview *thetableview;
@property (nonatomic, strong) mytableviewdatasource *thedatasource;
@property (nonatomic, strong) myviewcontroller *thecontroller;
@property (nonatomic, strong) fakemyjumper *fakejumper;
@property (nonatomic, copy) nsstring *celljumpmethod;
@property (nonatomic, strong) id datapassedtojumper;

@end

@implementation myviewcontrollertests

- (void)setup {
    [super setup];
    self.thetableview = [[uitableview alloc] init];
    self.thedatasource = [[mytableviewdatasource alloc] init];
    self.thecontroller = [[myviewcontroller alloc] init];
}

- (void)teardown {
    self.thedatasource = nil;
    self.thetableview = nil;
    self.thecontroller = nil;
    self.fakejumper = nil;
    self.celljumpmethod = nil;
    self.datapassedtojumper = nil;
    [super teardown];
}

/**
 选择一种数据类型的cell

 @param type <#type description#>
 */
- (void)selectcellwithdatatype:(modeltype)type{
    uinavigationcontroller *navvc = [[uinavigationcontroller alloc] init];
    self.fakejumper = [[fakemyjumper alloc] initwithnavigationcontroller:navvc];
    __weak typeof(self) wself = self;
    self.fakejumper.callmethodblock = ^(nsstring *methodname, nsdictionary *parameters) {
        __strong typeof(self) sself = wself;
        sself.celljumpmethod = methodname;
        sself.datapassedtojumper = parameters[[fakemyjumper modelkey]];
    };
    self.thecontroller.thejumper = self.fakejumper;
    self.thedatasource.thedataarray = @[@{@"type":@0,@"title":@"type a title",@"someid":@"0001"},@{@"type":@1,@"title":@"type b title",@"someid":@"0002"},@{@"type":@2,@"title":@"type c title",@"someid":@"0003"}];
    self.thecontroller.thedatasource = self.thedatasource;
    [self.thecontroller viewdidload];
    nsinteger row = 0;
    if (type == modeltypea) {
        row = 0;
    }else if (type == modeltypeb){
        row = 1;
    }else if (type == modeltypec){
        row = 2;
    }
    [self.thedatasource tableview:self.thetableview didselectrowatindexpath:[nsindexpath indexpathforrow:row insection:0]];
}

【tc4.8,tc 5.15,tc 5.16,tc 5.17】几个测试用例将变得很简洁,而且测试意图也更明显:
myviewcontrollertests.m文件:

/**
 tc 4.8
 */
- (void)test_selectacellthejumpercalljumpmethod{
    [self selectcellwithdatatype:modeltypec];
    xctasserttrue([self.celljumpmethod isequaltostring:[fakemyjumper jumpmethodname]]);
}

/**
 tc 5.15
 */
- (void)test_selectatypecellpassatypemodeltothejumper{
    [self selectcellwithdatatype:modeltypea];
    xctasserttrue([self.celljumpmethod isequaltostring:[fakemyjumper jumpmethodname]]);
    xctasserttrue([self.datapassedtojumper iskindofclass:[mymodel class]]);
    mymodel *datamodel = self.datapassedtojumper;
    xctasserttrue(datamodel.type == modeltypea);
}

/**
 tc 5.16
 */
- (void)test_selectbtypecellpassbtypemodeltothejumper{
    [self selectcellwithdatatype:modeltypeb];
    xctasserttrue([self.celljumpmethod isequaltostring:[fakemyjumper jumpmethodname]]);
    xctasserttrue([self.datapassedtojumper iskindofclass:[mymodel class]]);
    mymodel *datamodel = self.datapassedtojumper;
    xctasserttrue(datamodel.type == modeltypeb);
}

/**
 tc 5.17
 */
- (void)test_selectctypecellpassctypemodeltothejumper{
    [self selectcellwithdatatype:modeltypec];
    xctasserttrue([self.celljumpmethod isequaltostring:[fakemyjumper jumpmethodname]]);
    xctasserttrue([self.datapassedtojumper iskindofclass:[mymodel class]]);
    mymodel *datamodel = self.datapassedtojumper;
    xctasserttrue(datamodel.type == modeltypec);
}

运行全部测试用例,没有一个失败,说明这次重构没问题。

这篇文章到这里就结束了,现在产品代码的控制器里面没有关联mymodel类,没有关联atypeviewcontroller,btypeviewcontroller,ctypeviewcontroller类,但是却能够根据选择不同的cell,执行不同的跳转。所以,到现在为止,我们的控制器已经跟我们模块专有的cell的跳转逻辑解耦了,它里面不会存有跟模块相关的专门的cell跳转的业务逻辑代码了,我实现了篇头所说的我想要的重构方案。这种重构方案不仅让产品代码具备更好的设计性,而且让产品重要的业务逻辑变得更容易测试。