【问题标题】:How can I test my class which uses UIWebView如何测试使用 UIWebView 的类
【发布时间】:2014-02-13 05:09:24
【问题描述】:

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

这是我要测试的类:

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

感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

    您到底想测试什么?按下按钮会导致 webview 加载 URL 还是 webview 实际带来 URL 内容?

    【讨论】:

      猜你喜欢
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      • 2020-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多