首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mocha、should.js和断言异常

Mocha、should.js和断言异常
EN

Stack Overflow用户
提问于 2012-09-22 13:18:53
回答 2查看 5.7K关注 0票数 8

我有一个文件app.coffee

代码语言:javascript
复制
class TaskList

class Task
    constructor: (@name) ->
        @status = 'incomplete'
    complete: ->
        if @parent? and @parent.status isnt 'completed'
          throw "Dependent task '#{@parent.name}' is not completed."
        @status = 'complete'
        true
    dependsOn: (@parent) ->
        @parent.child = @
        @status = 'dependent'

# Prepare scope stuff
root = exports ? window
root.TaskList = TaskList
root.Task = Task

和一个名为test/taskTest.coffee的文件

代码语言:javascript
复制
{TaskList, Task} = require '../app'
should = require 'should'

describe 'Task Instance', ->
    task1 = task2 = null
    it 'should have a name', ->
        something = 'asdf'
        something.should.equal 'asdf'
        task1 = new Task 'feed the cat'
        task1.name.should.equal 'feed the cat'
    it 'should be initially incomplete', ->
        task1.status.should.equal 'incomplete'
    it 'should be able to be completed', ->
        task1.complete().should.be.true
        task1.status.should.equal 'complete'
    it 'should be able to be dependent on another task', ->
        task1 = new Task 'wash dishes'
        task2 = new Task 'dry dishes'
        task2.dependsOn task1
        task2.status.should.equal 'dependent'
        task2.parent.should.equal task1
        task1.child.should.equal task2
    it 'should refuse completion it is dependent on an uncompleted task', ->
        (-> task2.complete()).should.throw "Dependent task 'wash dishes' is not completed."

如果我在terminal:mocha -r should --compilers coffee:coffee-script -R spec中运行这个命令,我会得到一个失败的测试(最后一个),说明它预期会出现一个异常“相关任务‘洗碗’没有完成”。但是得到了“未定义的”。

如果通过删除括号将(-> task2.complete()).should.throw更改为-> task2.complete().should.throw,则测试将通过,如果不抛出异常,则测试将失败。但是,如果我将异常消息更改为随机消息,它仍然会通过。我做错了什么吗?难道测试不应该只有在消息字面上是“依赖任务‘洗碗’没有完成”时才通过吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-25 06:40:40

您正在使用字符串抛出异常,而不是抛出错误对象。throw()寻找后者。所以如果你这样做了,你的原始代码就可以工作了:

代码语言:javascript
复制
throw new Error "Dependent task '#{@parent.name}' is not completed."

如果您用CoffeeScript编写的内容产生的结果毫无意义,请尝试将其编译为js (或将代码粘贴到try CoffeeScript中。您将看到:

代码语言:javascript
复制
-> task2.complete().should.throw "Dependent task 'wash dishes' is not completed."

编译为:

代码语言:javascript
复制
(function() {
  return task2.complete().should["throw"]("Dependent task 'wash dishes' is not completed.");
});

它只是定义了一个函数,并不执行它。这解释了为什么更改字符串不会有什么不同。我希望这能有所帮助。

票数 4
EN

Stack Overflow用户

发布于 2012-09-25 10:24:37

首先,这是一些好看的咖啡脚本。

其次,David Weldon在他的回答中是正确的,你只需要改变投掷来真正抛出一个错误,它就会起作用。

这是你放入一个长文件中的代码,只是抛出的内容发生了变化。

代码语言:javascript
复制
class TaskList

class Task
    constructor: (@name) ->
        @status = 'incomplete'
    complete: ->
        if @parent? and @parent.status isnt 'completed'
          throw new Error "Dependent task '#{@parent.name}' is not completed."
        @status = 'complete'
        true
    dependsOn: (@parent) ->
        @parent.child = @
        @status = 'dependent'

# Prepare scope stuff
root = exports ? window
root.TaskList = TaskList
root.Task = Task

should = require 'should'

describe 'Task Instance', ->
    task1 = task2 = null
    it 'should have a name', ->
        something = 'asdf'
        something.should.equal 'asdf'
        task1 = new Task 'feed the cat'
        task1.name.should.equal 'feed the cat'
    it 'should be initially incomplete', ->
        task1.status.should.equal 'incomplete'
    it 'should be able to be completed', ->
        task1.complete().should.be.true
        task1.status.should.equal 'complete'
    it 'should be able to be dependent on another task', ->
        task1 = new Task 'wash dishes'
        task2 = new Task 'dry dishes'
        task2.dependsOn task1
        task2.status.should.equal 'dependent'
        task2.parent.should.equal task1
        task1.child.should.equal task2
    it 'should refuse completion it is dependent on an uncompleted task', ->
        (-> task2.complete()).should.throw "Dependent task 'wash dishes' is not completed."

摩卡,那个混蛋,你可以走了。

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

https://stackoverflow.com/questions/12540948

复制
相关文章

相似问题

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