开发者

iPhone UnitTesting UITextField value and otest error 133

Are UITextFields not meant to be part of the LogicTests and instead part of the ApplicationTest target?

I have a factory class that is responsible for creating and returning an (iPhone) UITextField and I'm trying to unit test it. It is part of my Logic Test target and when I try to build and run the tests, I get a build error about:

/Developer/Tools/RunPlatformUnitTests.include:451:0 Test rig '/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/ 'Developer/usr/bin/otest' exited abnormally with code 133 (it may have crashed).

In the build window, this points to the following line in: "RunPlatformUnitTests.include"

RPUTIFail ${LINENO} "Test rig '${TEST_RIG}' exited abnormally with code ${TEST_RIG_RESULT} (it may have crashed)."

My unit test looks like this:

#import <SenTestingKit/SenTestingKit.h>
#import <UIKit/UIKit.h>

// Test-subject headers.
#import "TextFieldFactory.h"

@interface TextFieldFactoryTests : SenTestCase {    

}
@end

@implementation TextFieldFactoryTests

#pragma mark Test Setup/teardown
- (void) setUp {
    NSLog(@"%@ setUp", self.name);      
}

- (void) tearDown {
    NSLog(@"%@ tearDown", self.name);
}

#pragma mark Tests
- (void) testUITextField_NotASecureField
{
    NSLog(@"%@ start", self.name);   
    UITextField *textField = [TextFieldFactory createTextField:YES];    
    NSLog(@"%@ end", self.name); 
}

The class I'm trying to test:

// Header file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface TextFieldFactory : NSObject {

}

+(UITextField *)createTextField:(BOOL)isSecureField;

@end

// Implementation file
#import "TextFieldFactory.h"

@implementation TextFieldFactory

+(UITextField *)createTextField:(BOOL)isSecureField                   
{
    // x,y,z,w are constants declared else where   
    UITextField *textField = [[[UITextField alloc] 
            initWithFrame:CGRectMake(x, y, z, w)] 
            autorelease];

    // some initia开发者_开发技巧lization code

    return textField;   
}

@end


This is a duplicate of

Why does instantiating a UIFont in an iphone unit test cause a crash?

Here's the backtrace:

#0  0x004899d1 in __HALT ()
#1  0x003d576d in _CFRuntimeCreateInstance ()
#2  0x00c57ef0 in GSFontCreateWithName ()
#3  0x03fccc72 in +[UIFont systemFontOfSize:] ()
#4  0x03f80766 in +[UILabel defaultFont] ()
#5  0x03f82ec4 in -[UILabel _commonInit] ()
#6  0x03f80d3a in -[UILabel initWithFrame:] ()
#7  0x03efbde8 in -[UITextField createTextLabelWithTextColor:] ()
#8  0x03f03e1a in -[UITextField initWithFrame:] ()
#9  0x00c52d75 in +[TextFieldFactory createTextField:] (self=0xc5308c, _cmd=0x8400b50, isSecureField=1 '\001') at /Users/dmaclach/Desktop/test/Classes/untitled2.m:19
#10 0x00c52c98 in -[TextFieldFactoryTests testUITextField_NotASecureField] (self=0x2161bf0, _cmd=0xc52dfe) at /Users/dmaclach/Desktop/test/Classes/untitled.m:26
#11 0x0043642d in __invoking___ ()
#12 0x00436301 in -[NSInvocation invoke] ()
#13 0x20104632 in -[SenTestCase invokeTest] ()
#14 0x20104d07 in -[SenTestCase performTest:] ()
...


Apparently, you are not supposed to include UITextField for your logic tests but move them into your application tests.


So I have the same problem too.....any solutions?

What i do to resolve it is create a DummyUITextField class and inject that into my classes that need a UITextField. (its like a mock, but mock would be wrong terminology).

@interface DummyUITextField : UIView {

NSString *text;

}

@property (nonatomic, retain) NSString *text;

@end

You can then add any other methods you need to this class.

However, if the code you're testing creates it's own UITextField's (instead of using Interface Builder), then you're going to have to find another solution.

Thanks Dave

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜