मैं एक फ़ाइल app.coffee
है जोर देते हुए:मोचा, should.js और एक अपवाद
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''' हैं? मैं प्रत्येक परीक्षण चरण में गुणों को दोबारा शुरू कर दूंगा। आप अपने परीक्षण में पहले से उपयोग कर सकते हैं। – vik
@vik हाँ यह 'parent.name' है। मैंने प्रत्येक संपत्ति को पहले() में फिर से चलाने की कोशिश की और अभी भी एक ही समस्या है। अंतिम दावा 'अपरिभाषित' हो जाता है। – Matthew