GHAssertThrowsSpecific 找不到类型 NSRangeException
我第一次使用 Xcode 4 和 GHUnit 编写一些单元测试。所有建议似乎都建议使用 GHUnit 而不是 OCUnit。
我有一个名为“myList”的自定义集合对象,并传递一条消息以获取 index:-1
处的选择。因此,它正确地抛出 NSRangeException (来自底层可变数组)。
我正在努力用 GHAssertThrowsSpecific 断言来捕捉这一点。
以下代码行不会编译并显示“未知类型名称‘NSRangeException’”。
GHAssertThrowsSpecific(s = [myList selectionAtIndex:-1],
NSRangeException, @"Should have thrown an NSRangeException", nil);
我正在 #importing "Foundation/NSException.h"
,其中似乎定义了 NSRangeException 。如果我将其更改为:
GHAssertThrowsSpecific(s = [myList selectionAtIndex:-1],
NSException, @"Should have thrown an NSException", nil);
then 编译正常并且断言有效,因此它与 NSRangeException 有关。
如果我查看标头,NSRangeException 似乎被定义为 NSString * const
在这种情况下,我如何尝试断言我期望捕获它。
我显然很愚蠢,因为我看不出我做错了什么。
I'm using Xcode 4 and GHUnit to write some unit tests for the first time. All the advice appears to suggest going with GHUnit and not OCUnit.
I have a custom collection object called 'myList', and passing a message to get the selection at index:-1
. It therefore correctly throws an NSRangeException (from the underlying mutable array).
I'm struggling to catch this with a GHAssertThrowsSpecific assertion.
This following line of code will not compile saying 'Unknown type name 'NSRangeException'.
GHAssertThrowsSpecific(s = [myList selectionAtIndex:-1],
NSRangeException, @"Should have thrown an NSRangeException", nil);
I am #importing "Foundation/NSException.h"
where NSRangeException appears to be defined. If I change it to:
GHAssertThrowsSpecific(s = [myList selectionAtIndex:-1],
NSException, @"Should have thrown an NSException", nil);
then compiles fine and the assertion works, so its something to do with NSRangeException.
If I look in the headers, NSRangeException appears to be defined as a NSString * const
in which case, how do I try to assert that I am expecting to catch it.
I'm obviously being quite dumb, as I can't see what I'm doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,所以我找到了这个问题的答案。
NSRangeException 实际上只是一个指向字符串的指针,其中包含“NSRangeException”。
我应该使用 GHAssertThrowsSpecificNamed,而不是使用 GHAssertThrowsSpecific,它采用指定异常字符串的附加参数,如下所示:
这有效。
Ok, so I found the answer to this one.
NSRangeException is indeed just a pointer to a string, which contains "NSRangeException".
Instead of using GHAssertThrowsSpecific, I should have been using GHAssertThrowsSpecificNamed, which takes an additional parameter of the string of the named exception, as follows:
This works.