Skip to content

Instantly share code, notes, and snippets.

@gajus
Last active August 29, 2015 14:06
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gajus/2a7fd107f897213aee67 to your computer and use it in GitHub Desktop.
// The hash (#) and dot (.) symbols are a convention to mark if a method
// is called on an individual object or on the prototype. ".create" is
// called on the Cart prototype. #add are called on the derived objects.
describe('Cart', function () {
var cart;
beforeEach(function () {
cart = Cart.create();
});
describe('.create', function () {
it('must create a cart that contains 0 products', function () {
var cart = Cart.create();
expect(cart.numProducts()).toEqual(0);
});
});
describe('#add', function () {
it('must add a product to the cart', function () {
var product = {};
cart.add(product);
expect(cart.doesContain(product)).toBeTruthy();
});
});
describe('#doesContain', function () {
it('must return false for a product that is not contained', function () {
var product = {};
cart.add({});
expect(cart.doesContain(product)).toBeFalsy();
});
});
describe('#grossPriceSum', function () {
it('must return 0 for an empty cart', function () {
expect(cart.grossPriceSum()).toBe(0);
});
it('must return price for a single product in the cart', function () {
var product;
product = {
grossPrice: function () { return 0; }
};
spyOn(product, 'grossPrice').and.returnValue(10);
cart.add(product);
expect(cart.grossPriceSum()).toBe(10);
});
it('must return price for two products in the cart', function () {
cart.add({grossPrice: function () { return 10; }});
cart.add({grossPrice: function () { return 20; }});
expect(cart.grossPriceSum()).toBe(30);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment