(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.");
  }

  代码16,测试通过URL生成UMViewController的用例
  一个测试方法多次断言
  除了一个操作需要多个测试方法的情况,在同一个测试方法中也会有对一个结果进行多次断言的情况(上述用例代码16中已经是这种情况,一下用例更具代表性)。这种情况发生在操作结果较为复杂的情况下,例如生成一个UMNavigationController(代码17)是这种情况:UMNavigationController的初始化方法是带RootViewController参数的,所以初始化的实例除了判断其本身是否为UINavigationController的子类和UMNavigationController实例外,还要判断rootViewController的合法性,以及viewControllers数组的正确性。

  (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));
  }

  代码17,测试生成UMNavigationController的用例


  总结
  本文一共取了URLManager中的17段代码片段作为例子,介绍了从利用测试框架提供的断言方法进行简单的测试,一直到使用自定义匹配引擎规则创建较复杂测试用例,并且提到了部分测试引擎和匹配引擎使用过程中会遇到的陷阱。旨在推动开发者能够在开发过程中更简单高效的使用单元测试,为提升代码质量增加一份保障。读者可以在URLManager的工程中阅读更多的测试用例代码。