为什么我的类崩溃了我的OCUnit测试用例,错误代码为138?

时间:2012-10-31 18:46:21

标签: objective-c ios unit-testing ocunit

我编写的一个类正在崩溃我的测试用例,错误代码为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测试中的第一个testUserAgentString工作正常但第二个STAssertNotNil是导致测试崩溃的行。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您的项目未正确设置以进行单元测试。试试checking these settings。如果失败,请尝试创建一个新项目,选择“包含单元测试”。