【问题标题】:Squeak - SUnit Testing for ErrorsSqueak - 错误的 SUnit 测试
【发布时间】:2010-03-28 06:30:20
【问题描述】:

有人建议我在我的测试用例中使用 should:rise 来测试方法可能引发的错误。由于某种原因,它没有按预期工作,所以我想验证我做对了。下面是测试用例中的代码:

self should: [aMyClass compareTo: 'This is a string'] raise: 'invalid input'.

我的 compareTo/1 方法如下所示:

(aMyClass isKindOf: MyClass) ifFalse: [self error: 'invalid input'.].

测试运行器的输出是“1 个错误”。

谢谢。

【问题讨论】:

    标签: squeak


    【解决方案1】:

    #should:raise: 期望 Exception 类作为其第二个参数,类似于异常处理中 #on:do: 的第一个参数:

     self should: [ aMyClass compareTo: 'This is a string' ] raise: Error
    

    【讨论】:

    • 如何区分 compareTo 可能引发的不同错误?刚开始学squeak,对错误/异常机制还不熟悉。
    • 为了区分错误嵌套处理程序:[ [ aMyClass compareTo: 'This is a string' ] on: ErrorType1 do: [ :err | ... ] ] on: ErrorType2 do: [ :err | ... ]。查看 Pharo by Example 2 (pharobyexample.org) 关于异常的章节草案:gforge.inria.fr/frs/download.php/26600/…
    猜你喜欢
    • 1970-01-01
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    • 2019-04-20
    • 2019-01-09
    相关资源
    最近更新 更多