iOS开发中的单元测试(三)——URLManager中的测试用例解析
本文转载至 http://www.cocoachina.com/cms/plus/view.php?aid=8088
- #import
- @interface UMTestCase : GHTestCase
- @end
- 代码2,定义属性// 普通字符串,带有字母和数字
- @property (strong, nonatomic) NSString *string;
- // 普通字符串,仅带有字母
- @property (strong, nonatomic) NSString *stringWithoutNumber;
- // 将被做URLEncode的字符串,含有特殊字符和汉字
- @property (strong, nonatomic) NSString *toBeEncode;
- // 把 toBeEncode 编码后的串
- @property (strong, nonatomic) NSString *encoded;
- // 普通的URL,带有QueryString
- @property (strong, nonatomic) NSURL *url;
- // 去掉上边一个URL的QueryString
- @property (strong, nonatomic) NSURL *noQueryUrl;
- // 一个普通的UIView
- @property (strong, nonatomic) UIView *view;
- (void)setUpClass
- {
- self.string = @"NSString For Test with a number 8848.";
- self.stringWithoutNumber = @"NSString For Test.";
- self.toBeEncode = @"~!@#$%^&*()_+=-[]{}:;\"'<>.,/?123qwe汉字";
- self.encoded = @"%7E%21%40%23%24%25%5E%26%2A%28%29_%2B%3D-%5B%5D%
- 7B%7D%3A%3B%22%27%3C%3E.%2C%2F%3F123qwe%E6%B1%89%E5%AD%97";
- self.url = [NSURL URLWithString:@"http://example.com
- /patha/pathb/?p2=v2&p1=v1"];
- self.noQueryUrl = [NSURL URLWithString:@"http://example.com
- /patha/pathb/"];
- self.view = [[UIView alloc] initWithFrame:CGRectMake(10.0f,
- 10.0f, 100.0f, 100.f)];
- }
- #pragma mark - UMString
- - (void)testUMStringContainsString
- {
- NSString *p = @"For";
- NSString *np = @"BAD";
- GHAssertTrue([self.string containsString:p],
- @"\"%@\" should contains \"%@\".",
- self.string, p);
- GHAssertFalse([self.string containsString:np],
- @"\"%@\" should not contain \"%@\".",
- self.string, p);
- (void)testUrlencode
- {
- GHAssertEqualStrings([self.toBeEncode urlencode], self.encoded,
- @"URLEncode Error.",
- self.toBeEncode, self.encoded);
- GHAssertEqualStrings([self.encoded urldecode], self.toBeEncode,
- @"URLDecode Error.",
- self.encoded, self.toBeEncode);
- }
- #pragma mark - UMURL
- - (void)testAddParams
- {
- NSURL *queryUrl = [self.noQueryUrl addParams:@{@"p1":@"v1",@"p2":@"v2"}];
- HC_assertThat(queryUrl.absoluteString, HC_containsString(@"p1=v1"));
- HC_assertThat(queryUrl.absoluteString, HC_containsString(@"p2=v2"));
- }
- (void)testRemoveAllSubviews
- {
- UIView *subViewA = [[UIView alloc] init];
- UIView *subViewB = [[UIView alloc] init];
- [self.view addSubview:subViewA];
- [self.view addSubview:subViewB];
- HC_assertThat(self.view.subviews, HC_containsInAnyOrder(subViewA, subViewB, nil));
- [self.view removeAllSubviews];
- if (nil != self.view.subviews) {
- HC_assertThat(self.view.subviews, HC_empty());
- }
- }
- (void)testAddConfig
- {
- [UMNavigationController setViewControllerName:@"ViewControllerA" forURL:@"
- um://viewa2"];
- NSMutableDictionary *config = [UMNavigationController config];
- NSLog(@"%@", [config allKeys]);
- HC_assertThat([config allKeys],
- HC_containsInAnyOrder(HC_equalTo(@"um://viewa2"), HC_equalTo(@"
- um://viewa"),
- HC_equalTo(@"um://viewb"), nil));
- GHAssertEqualStrings(config[@"um://viewa2"], @"ViewControllerA",
- @"config set error.");
- }
- (BOOL)matches:(id)item
- {
- NSUInteger index = 0;
- for (id matcher in matchers)
- {
- if ([matcher matches:item])
- {
- [matchers removeObjectAtIndex:index];
- return YES;
- }
- ++index;
- }
- [[mismatchDescription appendText:@"not matched: "] appendDescriptionOf:item];
- return NO;
- }
- - (BOOL)matches:(id)collection describingMismatchTo:(id)
- mismatchDescription
- {
- if (![collection conformsToProtocol:@protocol(NSFastEnumeration)])
- {
- [super describeMismatchOf:collection to:mismatchDescription];
- return NO;
- }
- HCMatchingInAnyOrder *matchSequence =
- [[HCMatchingInAnyOrder alloc] initWithMatchers:matchers
- mismatchDescription:mismatchDescription];
- for (id item in collection)
- if (![matchSequence matches:item])
- return NO;
- return [matchSequence isFinishedWith:collection];
- }
- @implementation HCIsCollectionHavingInAnyOrder
- - (BOOL)matches:(id)collection describingMismatchTo:(id)
- mismatchDescription
- {
- if (![collection conformsToProtocol:@protocol(NSFastEnumeration)])
- {
- [super describeMismatchOf:collection to:mismatchDescription];
- return NO;
- }
- HCMatchingInAnyOrderEx *matchSequence =
- [[HCMatchingInAnyOrderEx alloc] initWithMatchers:matchers
- mismatchDescription:mismatchDescription];
- for (id item in collection)
- if (![matchSequence matches:item])
- return NO;
- return [matchSequence isFinishedWith:collection];
- }
- @end
- id HC_hasInAnyOrder(id itemMatch, ...)
- {
- NSMutableArray *matchers = [NSMutableArray arrayWithObject:HCWrapInMatcher
- (itemMatch)];
- va_list args;
- va_start(args, itemMatch);
- itemMatch = va_arg(args, id);
- while (itemMatch != nil)
- {
- [matchers addObject:HCWrapInMatcher(itemMatch)];
- itemMatch = va_arg(args, id);
- }
- va_end(args);
- return [HCIsCollectionHavingInAnyOrder isCollectionContainingInAnyOrder:matchers];
- }
- (BOOL)matches:(id)item
- {
- NSUInteger index = 0;
- BOOL matched = (0 >= [self.matchers count]);
- for (id matcher in self.matchers)
- {
- if ([matcher matches:item]) {
- [self.matchers removeObjectAtIndex:index];
- matched = YES;
- return YES;
- }
- ++index;
- }
- return matched;
- }
- (void)testAddConfig
- {
- [UMNavigationController setViewControllerName:@"ViewControllerA" forURL:@"um://
- viewa2"];
- NSMutableDictionary *config = [UMNavigationController config];
- HC_assertThat([config allKeys],
- HC_hasInAnyOrder(HC_equalTo(@"um://viewa2"), nil));
- GHAssertEqualStrings(config[@"um://viewa2"], @"ViewControllerA",
- @"config set error.");
- }
- #pragma mark - UMView
- HC_assertThat(NSStringFromCGSize(self.view.size),
- HC_equalToSize(self.view.frame.size));
- HC_assertThat(NSStringFromCGPoint(self.view.origin),
- HC_equalToPoint(CGPointMake(self.view.frame.origin.x, self.
- view.frame.origin.y)));
- #import
- OBJC_EXPORT id HC_equalToPoint(CGPoint point);
- #ifdef HC_SHORTHAND
- #define equalToPoint HC_equalToPoint
- #endif
- @interface HCIsEqualToPoint : HCBaseMatcher
- + (id)equalToPoint:(CGPoint)point;
- - (id)initWithPoint:(CGPoint)point;
- @property (nonatomic, assign) CGFloat x;
- @property (nonatomic, assign) CGFloat y;
- @end
- #import "HCIsEqualToPoint.h"
- #import
- id HC_equalToPoint(CGPoint point)
- {
- return [HCIsEqualToPoint equalToPoint:point];
- }
- @implementation HCIsEqualToPoint
- + (id)equalToPoint:(CGPoint)point
- {
- return [[self alloc] initWithPoint:point];
- }
- - (id)initWithPoint:(CGPoint)point
- {
- self = [super init];
- if (self) {
- self.x = point.x;
- self.y = point.y;
- }
- return self;
- }
- - (BOOL)matches:(id)item
- {
- if (! [item isKindOfClass:[NSString class]]) {
- return NO;
- }
- CGPoint point = CGPointFromString((NSString *)item);
- return (point.x == self.x && point.y == self.y);
- }
- - (void)describeTo:(id)description
- {
- [description appendText:@"Point not equaled."];
- }
- @end
- (void)testViewControllerForSimpleURL
- {
- self.viewControllerA = (ViewControllerA *)[self.navigator
- viewControllerForURL:
- [NSURL URLWithString:@"um://viewa"]
- withQuery:nil];
- HC_assertThat(self.viewControllerA, HC_instanceOf([UMViewController class]));
- HC_assertThat(self.viewControllerA, HC_isA([ViewControllerA class]));
- }
- - (void)testViewControllerForURLWithArgs
- {
- self.viewControllerA = (ViewControllerA *)[self.navigator
- viewControllerForURL:[NSURL URLWithString:@"um://viewa?
- p1=v1&p2=v2"]
- withQuery:nil];
- HC_assertThat(self.viewControllerA, HC_instanceOf([UMViewController class]));
- HC_assertThat(self.viewControllerA, HC_isA([ViewControllerA class]));
- HC_assertThat([self.viewControllerA.params allKeys], HC_containsInAnyOrder
- (@"p1", @"p2", nil));
- GHAssertEqualStrings(self.viewControllerA.params[@"p1"], @"v1", @"param error.");
- GHAssertEqualStrings(self.viewControllerA.params[@"p2"], @"v2", @"param error.");
- }
- - (void)testViewControllerWithQuery
- {
- self.viewControllerA = (ViewControllerA *)[self.navigator
- viewControllerForURL:
- [NSURL URLWithString:@"um://viewa"]
- withQuery:@{@"k1":@"v1", @"k2":@"v2"}];
- HC_assertThat([self.viewControllerA.query allKeys], HC_containsInAnyOrder
- (@"k1", @"k2", nil));
- GHAssertEqualStrings(self.viewControllerA.query[@"k1"], @"v1", @"param error.");
- GHAssertEqualStrings(self.viewControllerA.query[@"k2"], @"v2", @"param error.");
- }
- - (void)testViewControllerForURLAndQuery
- {
- self.viewControllerA = (ViewControllerA *)[self.navigator
- viewControllerForURL:
- [NSURL URLWithString:@"um://viewa?p1=v1&p2=v2"]
- withQuery:@{@"k1":@"v1", @"k2":@"v2"}];
- HC_assertThat([self.viewControllerA.params allKeys], HC_containsInAnyOrder
- (@"p1", @"p2", nil));
- GHAssertEqualStrings(self.viewControllerA.params[@"p1"], @"v1", @"param error.");
- GHAssertEqualStrings(self.viewControllerA.params[@"p2"], @"v2", @"param error.");
- HC_assertThat([self.viewControllerA.query allKeys], HC_containsInAnyOrder
- (@"k1", @"k2", nil));
- GHAssertEqualStrings(self.viewControllerA.query[@"k1"], @"v1", @"param error.");
- GHAssertEqualStrings(self.viewControllerA.query[@"k2"], @"v2", @"param error.");
- }
- (void)testInitWihtRootViewControllerURL
- {
- UMNavigationController *navigator = [[UMNavigationController alloc]
- initWithRootViewControllerURL:[NSURL URLWithString:@"um://viewb"]];
- HC_assertThat(navigator, HC_instanceOf([UINavigationController class]));
- HC_assertThat(navigator, HC_isA([UMNavigationController class]));
- HC_assertThat(navigator.rootViewController,
- HC_instanceOf([UMViewController class]));
- HC_assertThat(navigator.rootViewController, HC_isA([ViewControllerB class]));
- HC_assertThatInteger(navigator.viewControllers.count, HC_equalToInteger(1));
- HC_assertThat(navigator.viewControllers,
- HC_hasInAnyOrder(HC_instanceOf([UMViewController class]), nil));
- HC_assertThat(navigator.viewControllers,
- HC_hasInAnyOrder(HC_isA([ViewControllerB class]), nil));
- HC_assertThat(navigator.viewControllers,
- HC_hasInAnyOrder(HC_is(navigator.rootViewController), nil));
- }
iOS开发中的单元测试(三)——URLManager中的测试用例解析的更多相关文章
- iOS开发Swift篇—(三)字符串和数据类型
iOS开发Swift篇—(三)字符串和数据类型 一.字符串 字符串是String类型的数据,用双引号""包住文字内容 let website = "http://www ...
- iOS 开发问题集锦(三)
iOS 开发问题集锦(三) 介于群里大部分童鞋都是新手,为了大家能够更好的提问,并且提的问题能更好的得到回答,下面写几点提问时的注意事项: 1.认真对待你的问题,在提问题前有过认真的思考: 2.先在 ...
- iOS开发RunnLoop学习二:GCD中的定时器
#import "ViewController.h" @interface ViewController () /** 注释 */ @property (nonatomic, st ...
- iOS开发:XCTest单元测试(附上一个单例的测试代码)
测试驱动开发并不是一个很新鲜的概念了.在我最开始学习程序编写时,最喜欢干的事情就是编写一段代码,然后运行观察结果是否正确.我所学习第一门语言是c语言,用的最多的是在算法设计上,那时候最常做的事情就是编 ...
- iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流
上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo, 详情请看<iOS开发之窥探UICollectionViewControlle ...
- iOS开发小技巧--TableView Group样式中控制每个section之间的距离
一.TableView的Group样式中,默认的每个section都有sectionHeader和sectionFooter,只要调整这两个的大小就可以实现section之前的间距扩大或缩小 二.项目 ...
- iOS开发系列--扩展--播放音乐库中的音乐
众所周知音乐是iOS的重要组成播放,无论是iPod.iTouch.iPhone还是iPad都可以在iTunes购买音乐或添加本地音乐到音乐 库中同步到你的iOS设备.在MediaPlayer.fram ...
- iOS开发-从16进制颜色中获取UIColor
目前iOS中设置UIColor只能使用其枚举值.RGB等方法,不能直接将常用的16进制颜色值直接转为UIColor对象,所以写了点代码,将16进制颜色值转为UIColor. 代码如下, //头文件#i ...
- iOS开发读取plist文件、iphone中plist文件的
在Xcode中建立一个iOS项目后,会自己产生一个.plist文件,点击时会看见它显示的是类似于excel表格: 但是,如果打开方式选择Source Code,你会看见它其实是一个xml文件. 我们会 ...
- IOS开发中将定时器添加到runLoop中
runLoop主要就是为线程而生的.他能够让线程在有任务的时候保持工作状态,没有任务的时候让线程处于休眠待备状态. 主线程的runloop默认是开启的.主线程上创建的定时器已经默认添加到runLoop ...
随机推荐
- 微信小程序中 input组件影响页面样式的问题
input组件有个默认的宽高,好像是不能清除的,在使用flex布局的时候,发现会影响到页面的布局,以为是flex布局的问题,改为float布局试了下也是同样的问题,试着把input标签换成别的标签,问 ...
- jquery 同源跨域请求整理
//同源ajax请求数据 function getData(url,paramjson,fn) { $.ajax({ type : "POST", //提交方式 url : url ...
- hdu 3478 Catch 二分图染色
题目链接 题意 小偷逃跑,从某个点出发,每下一个时刻能够跑到与当前点相邻的点. 问是否存在某一个时刻,小偷可能在图中的任意一个点出现. 思路 结论 如果该图为连通图且不为二分图,则可能,否则不可能. ...
- 视音频数据处理入门:RGB、YUV像素数据处理【转】
转自:http://blog.csdn.net/leixiaohua1020/article/details/50534150 ==================================== ...
- html移动端 -- meta-模板 + rem
第一种方式: ps 不用除以2<header> <meta charset="utf-8"> <meta name="viewport&qu ...
- codevs贪吃的九头龙
传说中的九头龙是一种特别贪吃的动物.虽然名字叫“九头龙”,但这只是说它出生的时候有九个头,而在成长的过程中,它有时会长出很多的新头,头的总数会远大于九,当然也会有旧头因衰老而自己脱落.有一天,有M 个 ...
- Atcoder Grand Contest 024
A 略 B 略 C 略 D(构造分形) 题意: 给出一个由n个点的组成的树,你可以加一些点形成一个更大的树.对于新树中的两个点i和j,如果以i为根的树与以j为根的树是同构的那么i和j颜色可以相同.问最 ...
- 提高速度 history 的利用
history的介绍history是shell的内置命令,其内容在系统默认的shell的man手册中.history是显示在终端输入并执行的过命令,系统默认保留1000条.[root@localhos ...
- @RequestParam,@PathVariable,@ResponseBody,@RequestBody,@ModelAttribute学习
1.@RequestParam使用于参数上,用于将请求参数映射到指定参数变量上 例如: @RequestMapping(value="/hello",method=RequestM ...
- Java中的文件上传2(Commons FileUpload:commons-fileupload.jar)
相比上一篇使用Servlet原始去实现的文件上传(http://www.cnblogs.com/EasonJim/p/6554669.html),使用组件去实现相对来说功能更多,省去了很多需要配置和处 ...