Skip to content

Instantly share code, notes, and snippets.

@rodrigore
Created May 9, 2013 17:14
Show Gist options
  • Select an option

  • Save rodrigore/5548957 to your computer and use it in GitHub Desktop.

Select an option

Save rodrigore/5548957 to your computer and use it in GitHub Desktop.
Solución al problema de efectuar operaciones (solamente suma en este caso) entre dos tipos de monedas, haciendo uso de Javascript y Jasmine. La clase MoneyCalculator contiene el metodo *sum* que permiten operar entre dos objetos, cuyas propiedades son *monto* y *moneda*. La clase MoneyCalculator recibe como parametro un objeto *exchangeService* …
var MoneyCalculator = function (exchangeService) {
this.sum = function (a, b) {
if (a.currency !== b.currency) {
b.amount = exchangeService.convert(b, a.currency);
}
return {
"amount" : a.amount + b.amount,
"currency" : a.currency
};
};
};
describe("Sum of money", function() {
var moneyCalculator;
var firstParam;
var exchangeServiceFake;
beforeEach(function() {
exchangeServiceFake = jasmine.createSpyObj('exchangeService',['convert']);
moneyCalculator = new MoneyCalculator(exchangeServiceFake);
firstParam = {"amount": 200, "currency": "CLP"};
});
describe("Given 200 CLP and ", function() {
it("100 CLP, should be 300 CLP", function() {
var secondParam = {"amount": 100, "currency": "CLP"};
var result = moneyCalculator.sum(firstParam, secondParam);
var expected = {"amount": 300, "currency": "CLP"};
expect(expected).toEqual(result);
});
it("100.50 CLP, should be 300.50 CLP", function() {
var secondParam = {"amount": 100.50, "currency": "CLP"};
var result = moneyCalculator.sum(firstParam, secondParam);
var expected = {"amount": 300.50, "currency": "CLP"};
expect(expected).toEqual(result);
});
it("-100 CLP, should be 100 CLP", function() {
var secondParam = {"amount": -100, "currency": "CLP"};
var result = moneyCalculator.sum(firstParam, secondParam);
var expected = {"amount": 100, "currency": "CLP"};
expect(expected).toEqual(result);
});
it("0 CLP, should be 200 CLP", function() {
var secondParam = {"amount": 0, "currency": "CLP"};
var result = moneyCalculator.sum(firstParam, secondParam);
var expected = {"amount": 200, "currency": "CLP"};
expect(expected).toEqual(result);
});
it("10 CHF, should be 300 CLP", function() {
exchangeServiceFake.convert.andCallFake(function() {
return 100;
});
var secondParam = {"amount": 10, "currency": "CHF"};
var result = moneyCalculator.sum(firstParam, secondParam);
var expected = {"amount": 300, "currency": "CLP"};
expect(expected).toEqual(result);
expect(exchangeServiceFake.convert).toHaveBeenCalled();
expect(exchangeServiceFake.convert).toHaveBeenCalledWith(secondParam, "CLP");
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment