- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个带有 UIDatePicker
socket 的 UIViewController
,相应的 nib
文件,并在界面生成器中正确设置了 socket 。
我正在尝试运行以下单元测试(使用 OCHamcrest
匹配器库)。
- (void)test___datePicker___shouldBeConnected
{
[sut view];
assertThat(sut.datePicker, is(notNilValue()));
}
sut
(“被测系统”)是被单元测试的 View Controller 。
但是,我不断收到此运行时异常:
[UIPickerColumnView isDragging]: unrecognized selector sent to instance 0x10dcdcc70
UIPickerColumnView
不是我创建的类(它似乎是 UIKit
中使用的非公共(public)类)。
我试过从 nib 中删除 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
时抛出此异常?
最佳答案
我可以在单元测试时使用 UIDatePicker
确认此行为,似乎其他 iOS 开发人员也遇到此问题 ( https://discussions.apple.com/message/25373309)。
不幸的是,这似乎是 iOS 7 SKD (Xcode 5.1) 中的错误。但是,它似乎已在 iOS 8 SDK (Xcode 6 beta 1) 中修复。
如果您访问 UIViewController
的 view
属性并且它包含一个 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
socket 现在没有经过技术单元测试,但至少您可以继续对 View Controller 进行其余测试。
当然,请务必在切换到 Xcode 6 后立即放弃这种 try-catch 废话! :]
关于ios - 在单元测试 UIDatePicker 时获取运行时异常([UIPickerColumnView isDragging :] unrecognized selector),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24296319/
我创建了一个带有 UIDatePicker socket 的 UIViewController,相应的 nib 文件,并在界面生成器中正确设置了 socket 。 我正在尝试运行以下单元测试(使用 O
我是一名优秀的程序员,十分优秀!