8

私はファイルを持っています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と、テストは成功し、例外をスローしないと失敗します。しかし、例外メッセージをランダムなものに変更しても、それでも成功します。私は何か間違ったことをしていますか?メッセージが文字通り「依存タスク「皿洗い」が完了していない」である場合にのみ、テストに合格するべきではありませんか?

4

2 に答える 2

4

エラーオブジェクトをスローする代わりに、文字列を使用して例外をスローしています。throw()後者を探します。したがって、次の場合、元のコードは機能します。

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

CoffeeScriptで記述したものが意味のない結果を生成している場合は、それをjsにコンパイルしてみてください(または、 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.");
});

これは関数を定義するだけで、それを実行しません。これは、文字列を変更しても違いがない理由を説明しています。それがお役に立てば幸いです。

于 2012-09-24T22:40:39.533 に答える
3

まず、それは見栄えの良いCoffeescriptです。

第二に、David Weldonは、スローを変更して実際にエラーをスローするだけで機能するという彼の答えは正しいです。

これが、スローが変更された1つの長いファイルに入れられたコードです。

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

そのろくでなしのモカとあなたは行ってもいいです。

于 2012-09-25T02:24:37.100 に答える