【问题标题】:Mocha, should.js and asserting an exceptionMocha、should.js 和断言异常
【发布时间】:2012-09-14 11:47:23
【问题描述】:

我有一个文件app.coffee:

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的文件:

{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."

如果我在终端中运行此命令:mocha -r should --compilers coffee:coffee-script -R spec 我有一个失败的测试(最后一个)说它期待一个异常“相关任务'洗碗'未完成。”但得到了“未定义”。

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

【问题讨论】:

  • 你确定“洗碗”是parent.name吗?我会在每个测试步骤中重新声明属性。您可以在测试中使用 beforeEach。
  • @vik 是的,它是parent.name。我尝试在 beforeEach() 中重新声明每个属性,但仍然有同样的问题。最后的断言得到undefined

标签: node.js tdd coffeescript mocha.js should.js


【解决方案1】:

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

throw new Error "Dependent task '#{@parent.name}' is not completed."

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

-> task2.complete().should.throw "Dependent task 'wash dishes' is not completed."

编译为:

(function() {
  return task2.complete().should["throw"]("Dependent task 'wash dishes' is not completed.");
});

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

【讨论】:

  • 我的意见:添加括号来包裹函数体,会使coffeescript示例更清晰: (-> task2.complete()).should.throw "相关任务'洗碗'未完成。”
【解决方案2】:

首先,这是一些好看的 Coffeescript。

其次,David Weldon 的回答是正确的,您可以将 throw 更改为实际抛出错误并且它可以工作。

这是您的代码放入一个长文件中,只是更改了 throw。

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."

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多