首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TDD iOS - testNonConformingObjectCannotBeDelegate

TDD iOS - testNonConformingObjectCannotBeDelegate
EN

Stack Overflow用户
提问于 2014-03-20 11:58:27
回答 1查看 47关注 0票数 0

通过阅读测试驱动的iOS开发手册,我发现了这个测试,我正试着把它弄得头脑发热:

代码语言:javascript
复制
#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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-20 12:13:52

如果表达式不抛出异常,STAssertThrows将抛出异常。

setDelegate方法的StackOverflowManager是-

代码语言:javascript
复制
- (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将抛出一个异常,测试将失败。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22532264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档