2
\$\begingroup\$

I am going through some TDD and BDD tutorials which guide one through implementing a simple beverage-ordering system. While I think I understand the material, I am still wondering about some intricacies. In particular, it seems that my sinon.stub() line is not really doing anything.

I am soliciting comments on this code. Any suggestions for improvements are welcome. Those related to the test-driven development are especially so. How am I doing?

test.js

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

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


//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', () => {
        beforeEach( () => {
            this.orderId = 'some empty order id';
            this.orderDAO.byId.withArgs(this.orderId).returns([]);
            this.result = this.orderSystem.display(this.orderId);
        })
        it('will show no order items', () => {
            expect(this.result).to.have.property('items').that.is.empty;    
        });
        it('will show 0 as the total prince', () => {
            expect(this.result).to.have.property('totalPrice').that.is.equal(0);
        });
        it('will only be possible to add a beverage', () => {
            expect(this.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 order = function (orderDAO) {
    return {
        display: (orderID) => {return {
            "items": [],
            "totalPrice": 0,
            "actions": [{
                action:'append-beverage',
                target: orderID,
                parameters: {
                    beverageRef: null,
                    quantity: 0  
                }, 
            }],
        }},
    }
}

module.exports = order

Testing results:

  Customer displays order
    Given that the order is empty
      ✓ will show no order items
      ✓ will show 0 as the total prince
      ✓ will only be possible to add a beverage
    Given that the order contains beverages
      - will show one item per beverage
      - will show the sum of unit prices as total prince
      - will be possible to place the order
      - will be possible to add a beverage
      - will be possible to remove a beverage
      - will be possible to change the quantity of a beverage
    Given that the order has pending messages
      - will show the pending messages
      - there will be no more pending messages


  3 passing (249ms)
  8 pending
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.