通过阅读测试驱动的iOS开发手册,我发现了这个测试,我正试着把它弄得头脑发热:
#import "QuestionCreationTests.h"
#import "StackOverflowManager.h"
@implementation QuestionCreationTests {
@private
StackOverflowManager *mgr;
}
- (void)setUp {
mgr = [[StackOverflowManager alloc] init];
}
- (void)testNonConformingObjectCannotBeDelegate {
STAssertThrows(mgr.delegate =
(id <StackOverflowManagerDelegate>)[NSNull null],
@"NSNull should not be used as the delegate as doesn't"
@" conform to the delegate protocol");
}这将测试不符合条件的对象不能是委托。我的理解是,它使用NSNull作为一个样本不符合对象。然后将其转换为符合id协议的StackOverflowManagerDelegate类型的对象。然后检查它是否等于mgr.delegate。如果这会引发异常,那么它就无法通过测试。我的问题是:这怎么会引起例外呢?
有人能澄清吗?
如果有帮助的话,以下是序言:
应用程序将要求StackOverflowManager向其委托提供有关特定topic.That的问题,这意味着StackOverflowManager类必须有一个委托。
Fwiw,我知道我们现在会使用XCTAssertThrows。
发布于 2014-03-20 12:13:52
如果表达式不抛出异常,STAssertThrows将抛出异常。
setDelegate方法的StackOverflowManager是-
- (void)setDelegate:(id<StackOverflowManagerDelegate>)newDelegate {
if (newDelegate && ![newDelegate conformsToProtocol: @protocol(StackOverflowManagerDelegate)]) {
[[NSException exceptionWithName: NSInvalidArgumentException reason: @"Delegate object does not conform to the delegate protocol" userInfo: nil] raise];
}
delegate = newDelegate;
}由于NSNull不符合StackOverflowManagerDelegate协议,设置程序将抛出一个异常。STAssertThrows将捕获该异常并通过测试。如果setter没有抛出异常,那么STAssertThrows将抛出一个异常,测试将失败。
https://stackoverflow.com/questions/22532264
复制相似问题