2017-07-05 4 views
1

Ich habe den folgenden Code für meine Tests mit Sinon und Mocha. Jedes Mal, wenn ich diese Tests laufen lasse, erhalte ich die folgende zurückMocha und Sinon Tests mit Versprechen Timeout

0 passing (747ms) 
    8 pending 
    1 failing 

    1) Customer displays order Given that the order is empty "before each" hook for "will show no order items": 
    Error: Timeout of 500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. 

Die Prüfungen vorbei waren, bis ich die Versprechen Einbeziehung in das Bild gestartet und machen die Prüfung realistischer, dh eingerichtet mit asynchron beschäftigen Anrufe.

Was mache ich falsch und wie kann ich die Tests bestehen?

tests.js

'use strict'; 
require("babel-register"); 

var chai = require('chai'); 
var expect = chai.expect; 
var sinon = require('sinon'); 
var orderSystemWith = require('../lib/orders'); 

chai.use(require("chai-as-promised")); 


//describe is used to display features 
//context is used to display scenarios 
//it is used to describe tests within a feature/scenario 
describe('Customer displays order', function() { 
    beforeEach(() => { 
     this.orderDAO = { 
      byId: sinon.stub() 
     }; 
     this.orderSystem = orderSystemWith(this.orderDAO); 
    }) 
    context('Given that the order is empty',() => { 
     var result; 
     beforeEach((done) => { 
      this.orderId = 'some empty order id'; 
      this.orderDAO.byId 
       .withArgs(this.orderId) 
       .callsArgWithAsync(1, null, []); 

      return this.orderSystem.display(this.orderId) 
       .then(function (res){ 
        result = res 
       }) 
     }); 

     it('will show no order items',() => { 
      //expect(result).to.have.property('items').that.is.empty; 
      return expect(result).to.eventually.have.property('items').that.is.empty; 
     }); 
     it('will show 0 as the total prince',() => { 
      expect(result).to.have.property('totalPrice').that.is.equal(0); 
     }); 
     it('will only be possible to add a beverage',() => { 
      expect(result).to.have.property('actions').that.is.deep.equal([{ 
       action:'append-beverage', 
       target: this.orderId, 
       parameters: { 
        beverageRef: null, 
        quantity: 0 
       } 
      }]) 
     }); 
    }); 

    context('Given that the order contains beverages', function(){ 

     it('will show one item per beverage'); 
     it('will show the sum of unit prices as total prince'); 
     it('will be possible to place the order'); 
     it('will be possible to add a beverage'); 
     it('will be possible to remove a beverage'); 
     it('will be possible to change the quantity of a beverage'); 
    }); 

    context('Given that the order has pending messages', function(){ 
     it('will show the pending messages'); 
     it('there will be no more pending messages'); 
    }) 

}); 

orders.js

var Q = require('q'); 
    module.exports = function() { 
     return { 
      display: (orderId) => { 
       return Q.fulfill({ 
        items: [], 
        totalPrice: 0, 
        actions: [ 
        { 
         action: 'append-beverage', 
         target: orderId, 
         parameters: { 
          beverageRef: null, 
          quantity: 0 
         } 
        }]  
       }); 
      } 
     }; 
    }; 

Antwort

1

Ihr Versprechen gelöst ist nicht. Q.fulfill ist veraltet. wenn Sie ein aufgelöstes Versprechen Gebrauch wollen einfach zurück Q(value) Vom API reference:

Q (Wert)

Wenn value ein Q Versprechen ist, gibt das Versprechen.

Wenn value ein Versprechen von einer anderen Bibliothek ist, wird es in ein Q Versprechen (wo möglich) gezwungen.

Wenn value kein Versprechen ist, wird ein Versprechen zurückgegeben, das mit erfüllt ist.

Verwandte Themen