Unit testing in iOS development (III.) analysis of test cases in Urlmanager

Source: Internet
Author: User

Urlmanager is a navigation control based on Uinavigationcontroller and Uiviewcontroller, based on URL scheme, designed to achieve viewcontroller loose coupling without reliance.

Prepare the framework, define the base class

First, follow the methods described in the previous two articles to import the Unit test framework and the matching engine framework, set up test target, and configure compilation options.

Define test Case base class: Umtestcase (Code 1), all other use cases are inherited from Umtestcase.

#import <GHUnitIOS/GHTestCase.h>
@interface umtestcase:ghtestcase
@end

Code 1,umtestcase, use case base class

Building Use Cases

Urlmanager Tool Class (Umtools) test case (Umtoolstestcase). The nsurl,nsstring and UIView are extended in Umtools, which involves adding querystring to the URL and reading the parameters from the querystring, making substring judgments on strings, encoding and decoding the URL, UIView for X,y, Width and height of direct reading and writing. The properties (Code 2) are used in defining tests in the use case, and they are initialized in SetupClass (code 3).

Ordinary strings, with letters and digital
@property   (Strong, nonatomic)     nsstring    *string;
Ordinary string, with only the letter
@property   (Strong, nonatomic)     nsstring    *stringwithoutnumber;
A string that will be made urlencode, containing special characters and kanji
@property   (Strong, nonatomic)     nsstring    *tobeencode;
The Tobeencode encoded string
@property   (Strong, nonatomic)     nsstring    *encoded;
Generic URL with querystring
@property   (Strong, nonatomic)     nsurl       *url;
Remove the top URL of the QueryString
@property   (Strong, nonatomic)     nsurl       *noqueryurl;
An ordinary UIView
@property   (Strong, nonatomic)     UIView      *view;

Code 2, defining attributes

(void) SetupClass
{
    self.string                 = @ "NSString for Test with a number 8848.";
    Self.stringwithoutnumber    = @ "NSString for Test."
    Self.tobeencode             = @ "~!@#$%^&* () _+=-[]{}:;\" ' <>.,/?123qwe kanji ";
    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)];
}

Code 3, initializing properties

Using assertions in the Unit test framework to handle simple use cases

Unit test is a white box test, to do path coverage (code 4). The "containsstring" test is both forward and reverse (that is, yes and no two return results).

#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);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.