如何测试使用UIWebView的类

时间:2014-02-13 05:09:24

标签: ios objective-c uiwebview tdd gh-unit

enter image description here

我想做测试,但我不知道怎么做(我可以使用GHUnit)。

enter image description here

这是我要测试的课程:

Communicator.h

@interface Communicator : NSObject

-(void)loadURLWithString:(NSString*)urlString withWebView:(UIWebView*)webView;

@end

Communicator.m

#import "Communicator.h"

@implementation Communicator

##### I WANT TO EXCLUDE THE WEBVIEW BELOW #####
-(void)loadURLWithString:(NSString*)urlString withWebView:(UIWebView*)webView
{
    NSURL *url = [[NSURL alloc] initWithString:urlString];
    NSURLRequest *r = [[NSURLRequest alloc] initWithURL:url];
    [webView loadRequest:r];
}
@end

这是控制Communicator类的ViewController。

ViewController.h

#import "Communicator.h"

@interface ViewController : UIViewController <UITextFieldDelegate>
{
    Communicator *communicator;
}

@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIButton *startButton;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    communicator = [[Communicator alloc] init];
    [_startButton addTarget:self action:@selector(startButtonPushed) forControlEvents:UIControlEventTouchDown];
}

- (void)startButtonPushed
{
    [communicator loadURLWithString:@"http://www.apple.co.jp" withWebView:_webView];
}

@end

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

你想要测试的是什么?按下按钮会导致webview加载URL或webview实际带来URL内容吗?