2012-09-22 10 views
8

मैं एक फ़ाइल 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 को कोष्ठक को हटाकर बदलता हूं, परीक्षण पास होता है, और विफल रहता है तो मैं अपवाद नहीं फेंकता। लेकिन अगर मैं अपवाद संदेश को यादृच्छिक रूप से बदलता हूं, तो यह अभी भी गुजरता है। क्या मुझसे कुछ गलत हो रही है? अगर परीक्षण सचमुच "निर्भर कार्य" धोने वाले व्यंजनों को पूरा नहीं किया जाता है तो परीक्षण केवल तभी पास नहीं होना चाहिए। "

+0

क्या आप वाकई 'धोने वाले व्यंजन' '' parent.name''' हैं? मैं प्रत्येक परीक्षण चरण में गुणों को दोबारा शुरू कर दूंगा। आप अपने परीक्षण में पहले से उपयोग कर सकते हैं। – vik

+0

@vik हाँ यह 'parent.name' है। मैंने प्रत्येक संपत्ति को पहले() में फिर से चलाने की कोशिश की और अभी भी एक ही समस्या है। अंतिम दावा 'अपरिभाषित' हो जाता है। – Matthew

उत्तर

4

आप एक त्रुटि ऑब्जेक्ट फेंकने के बजाय स्ट्रिंग के साथ अपवाद फेंक रहे हैं। throw() उत्तरार्द्ध के लिए देखता है। तो अपने मूल कोड काम करता है अगर आप ऐसा करेंगे:

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

कुछ आप CoffeeScript में लिखने परिणाम है कि कोई मतलब उत्पादन किया जाता है, जे एस के लिए यह संकलन की कोशिश (या try CoffeeScript में कोड चिपकाकर दिखाई देंगे। कि:

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

जो सिर्फ एक समारोह को परिभाषित करता है और:

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

को संकलित इसे निष्पादित नहीं करता है। यह बताता है कि स्ट्रिंग को बदलने से कोई फर्क नहीं पड़ता। मुझे आशा है कि वह मदद करेंगे।

+0

मेरी राय: फ़ंक्शन बॉडी को लपेटने के लिए ब्रैकेट जोड़ें, कॉफ़ीस्क्रिप्ट नमूना अधिक स्पष्ट कर देगा: (-> task2.complete())। चाहिए। "निर्भर कार्य 'धोने वाले व्यंजन' पूरा नहीं हो पाएंगे।" –

3

सबसे पहले, यह कुछ अच्छी दिखने वाली कॉफ़ीस्क्रिप्ट है।

दूसरा, डेविड वेल्डन अपने जवाब में सही है कि आप वास्तव में एक त्रुटि फेंकने के लिए फेंक बदल सकते हैं और यह काम करता है।

यहां आपका कोड एक लंबी फ़ाइल में डाल दिया गया है जिसमें केवल फेंक दिया गया है।

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

मोचा जो बेस्टर्ड और आप जाने के लिए अच्छे हैं।

संबंधित मुद्दे