私が書いたクラスは、エラーコード138でテストケースをクラッシュさせています。このクラスは、UIWebViewからユーザーエージェント文字列を含むNSStringを返します。
@interface MyWebViewUserAgent : NSObject <UIWebViewDelegate> {
NSString* userAgent;
UIWebView* webView;
}
- (NSString*) userAgentString;
@end
#import "MyWebViewUserAgent.h"
@implementation MyWebViewUserAgent
- (NSString*) userAgentString {
if (userAgent != nil) return userAgent;
webView = [[UIWebView alloc] init];
webView.delegate = self;
[webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: @"http://127.0.0.1"]]];
// Wait for the web view to load our bogus request and give us the secret user agent.
while (userAgent == nil) {
// This executes another run loop.
[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate distantFuture]];
}
NSString *currentDeviceType = [[UIDevice currentDevice] model];
NSRange range = [userAgent rangeOfString:currentDeviceType];
if (range.location == NSNotFound) {
// Should only happen when iPhone-targeted app is running on an iPad.
NSRange rangeToReplace = [userAgent rangeOfString:@"iPhone;"];
if (rangeToReplace.length > 0) {
userAgent = [userAgent stringByReplacingCharactersInRange:rangeToReplace withString:@"iPad;"];
}
}
return userAgent;
}
- (BOOL) webView: (UIWebView*) web_view shouldStartLoadWithRequest: (NSURLRequest*) request navigationType: (UIWebViewNavigationType) navigation_type {
userAgent = [request valueForHTTPHeaderField: @"User-Agent"];
[webView release];
return NO; // Return no, we don't care about executing an actual request.
}
- (void) dealloc {
[super dealloc];
}
@end
さて、これが私のテストケースです:
#import <SenTestingKit/SenTestingKit.h>
#import <UIKit/UIKit.h>
#import "MyWebViewUserAgent.h"
@interface MyUnitTests : SenTestCase {
MyWebViewUserAgent *ua;
}
@end
@implementation MySDKUnitTests
- (void)setUp
{
[super setUp];
}
- (void)tearDown
{
// Tear-down code here.
[super tearDown];
}
- (void)testUserAgentString {
ua = [[MyWebViewUserAgent alloc] init];
STAssertNotNil(ua, @"User agent object is nil.");
STAssertNotNil([ua userAgentString], @"user agent string is nil");
[ua release];
}
@end
テストの最初STAssertNotNil
の行は正常に機能しますが、2番目の行はテストをクラッシュさせる行です。何か案は?testUserAgentString
STAssertNotNil