单元测试 UIDatePicker 时出现运行时异常([UIPickerColumnView isDragging:] 无法识别的选择器)

Posted

技术标签:

【中文标题】单元测试 UIDatePicker 时出现运行时异常([UIPickerColumnView isDragging:] 无法识别的选择器)【英文标题】:Getting runtime exception while unit testing UIDatePicker ([UIPickerColumnView isDragging:] unrecognized selector) 【发布时间】:2014-06-18 22:58:53 【问题描述】:

我创建了一个UIViewController 和一个UIDatePicker 出口,对应nib 文件,并在界面生成器中正确设置了出口。

我正在尝试运行以下单元测试(使用 OCHamcrest 匹配器库)。

- (void)test___datePicker___shouldBeConnected

  [sut view];
  assertThat(sut.datePicker, is(notNilValue()));

sut(用于“被测系统”)是被单元测试的视图控制器。

但是,我不断收到此运行时异常:

[UIPickerColumnView isDragging]: unrecognized selector sent to instance 0x10dcdcc70

UIPickerColumnView不是我创建的类(它似乎是UIKit 中使用的非公共类)。

我尝试从笔尖中删除 UIDatePicker 并以编程方式创建它:

- (void)viewDidLoad

  [super viewDidLoad];

  UIDatePicker *datePicker = [[UIDatePicker alloc] init];
  [datePicker setDate:[NSDate date]];
  [datePicker addTarget:self action:@selector(dateChanged:)
   forControlEvents:UIControlEventValueChanged];
  [self.view addSubview:datePicker];

但是,同样的异常发生了。

异常断点,这里是错误线程:

#0  0x0000000100857973 in objc_exception_throw ()
#1  0x0000000100c8065d in -[NSObject(NSObject) doesNotRecognizeSelector:] ()
#2  0x0000000100be0d8d in ___forwarding___ ()
#3  0x0000000100be0938 in __forwarding_prep_0___ ()
#4  0x00000001066c5b6e in -[UIPickerView _sendSelectionChangedFromTable:notify:] ()
#5  0x00000001066c5fec in -[UIPickerView _selectRow:inComponent:animated:notify:] ()
#6  0x0000000106c7bac7 in -[_UIDatePickerView _selectRow:inComponent:animated:notify:] ()
#7  0x0000000106c81c3f in -[_UIDatePickerMode loadDate:animated:] ()
#8  0x0000000106c85456 in -[_UIDatePickerMode_DateAndTime loadDate:animated:] ()
#9  0x0000000106c7ba8b in -[_UIDatePickerView _loadDate:animated:] ()
#10 0x0000000106c7b26b in -[_UIDatePickerView _setDate:animated:forced:] ()
#11 0x0000000106911c0e in -[UIDatePicker initWithCoder:] ()
#12 0x0000000106a6a794 in UINibDecoderDecodeObjectForValue ()
#13 0x0000000106a6a4df in -[UINibDecoder decodeObjectForKey:] ()
#14 0x000000010692e198 in -[UIRuntimeConnection initWithCoder:] ()
#15 0x0000000106a6a794 in UINibDecoderDecodeObjectForValue ()
#16 0x0000000106a6a95c in UINibDecoderDecodeObjectForValue ()
#17 0x0000000106a6a4df in -[UINibDecoder decodeObjectForKey:] ()
#18 0x000000010692d7b6 in -[UINib instantiateWithOwner:options:] ()
#19 0x00000001067c7b0c in -[UIViewController _loadViewFromNibNamed:bundle:] ()
#20 0x00000001067c8149 in -[UIViewController loadView] ()
#21 0x00000001067c83b7 in -[UIViewController loadViewIfRequired] ()
#22 0x00000001067c8777 in -[UIViewController view] ()
#23 0x0000000102e01c86 in -[AODateTimeDetailViewControllerTests test___datePicker___ShouldBeConnected] at /Users/Anthony/Documents/AORepos/AOReportControllers/AOReportControllersTests/AODateTimeDetailViewControllerTests.m:43
#24 0x0000000100be4f1c in __invoking___ ()
#25 0x0000000100be4dc4 in -[NSInvocation invoke] ()
#26 0x0000000102cb9c40 in -[XCTestCase invokeTest] ()
#27 0x0000000102cb9d2c in -[XCTestCase performTest:] ()
#28 0x0000000102cbaa75 in -[XCTest run] ()
#29 0x0000000102cb94df in -[XCTestSuite performTest:] ()
#30 0x0000000102cbaa75 in -[XCTest run] ()
#31 0x0000000102cb94df in -[XCTestSuite performTest:] ()
#32 0x0000000102cbaa75 in -[XCTest run] ()
#33 0x0000000102cb94df in -[XCTestSuite performTest:] ()
#34 0x0000000102cbaa75 in -[XCTest run] ()
#35 0x0000000102cbc1b4 in +[XCTestProbe runTests:] ()
#36 0x00000001000012e1 in ___lldb_unnamed_function9$$xctest ()
#37 0x0000000100001521 in ___lldb_unnamed_function11$$xctest ()
#38 0x0000000100001017 in ___lldb_unnamed_function2$$xctest ()

如何防止在单元测试UIDatePicker 时引发此异常?

【问题讨论】:

【参考方案1】:

我可以在单元测试时使用 UIDatePicker 确认此行为,其他 ios 开发人员似乎也遇到此问题 (https://discussions.apple.com/message/25373309)。

不幸的是,这似乎是 iOS 7 SKD (Xcode 5.1) 中的一个错误。不过,它似乎已在 iOS 8 SDK(Xcode 6 beta 1)中修复。

如果您访问UIViewControllerview 属性并且它包含UIDatePicker,则会引发此异常,这就是您通常在单元测试期间设置view 及其出口的方式。

这是我用来解决它的解决方案:

@interface YourViewControllerTests
@end

@implementation YourViewControllerTests

  YourViewController *sut;
  UIDatePicker *datePicker;


- (void)setUp

  [super setUp];

  sut = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:[NSBundle bundleForClass:[self class]]];
  [self givenViewSetup];


- (void)givenViewSetup

  @try 
    [sut view];
  
  @catch (NSException *exception) 
    // ignore the exception
  
  @finally 
    datePicker = [[UIDatePicker alloc] init];
    sut.datePicker = datePicker;
  


- (void)test___datePicker___shouldBeConnected

  assertThat(sut.datePicker, is(notNilValue()));


@end

虽然您的 UIDatePicker 插座现在在技术上没有经过单元测试,但至少您可以继续为您的视图控制器进行其余测试。

当然,一旦切换到 Xcode 6,请务必放弃这种尝试捕捉的废话! :]

【讨论】:

这确实暂时消除了错误,但是一旦您在 datePicker 上设置了日期,就会出现同样的错误。看起来这个错误只会阻止我们在 Xcode 6 之前进行单元测试。

以上是关于单元测试 UIDatePicker 时出现运行时异常([UIPickerColumnView isDragging:] 无法识别的选择器)的主要内容,如果未能解决你的问题,请参考以下文章

运行单元测试时出现语法错误后 PhantomJS 退出

使用 Roboelectric 3.8 运行单元测试时出现 NoClassDefFoundError

在 DataJPATest 下运行单元测试时出现问题

使用 Unitils 对 Postgres 数据库运行单元测试时出现 NullPointerException

ReferenceError:未定义窗口。当我通过 jest 运行 npm test 进行单元测试时出现此错误

仅在运行单元测试时出现 OLE DB 异常“类未注册”