<!DOCTYPE html>
<html>
<head>
<link data-require="jasmine@2.4.1" data-semver="2.4.1" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.css" />
<script data-require="jasmine@2.4.1" data-semver="2.4.1" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js"></script>
<script data-require="jasmine@2.4.1" data-semver="2.4.1" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine-html.js"></script>
<script data-require="jasmine@2.4.1" data-semver="2.4.1" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/boot.js"></script>
<script src="car.js"></script>
<script src="car.spec.js"></script>
</head>
<body>
<h1>Hello Jasmine!</h1>
</body>
</html>
Car = (function () {
function Car (engine) {
if (!engine) {
throw new TypeError('Engine type argument is required')
}
this.engine = engine;
this.driver = null;
this._passengers = [];
}
Car.prototype.drive = function (speed) {
if (typeof speed !== "number" || speed < 1) {
throw RangeError('Invalid driving speed');
}
this._lastSpeed = speed;
return 'Car with ' + this.engine + ' engine ' +
'drives at ' + speed + ' km/h.';
}
Object.defineProperty(Car.prototype, 'lastSpeed', {
get: function () {
return this._lastSpeed ? this._lastSpeed : 0;
},
enumerable: false,
configurable: true
});
Car.prototype.addPassenger = function (passenger) {
this._passengers.push(passenger);
}
Car.prototype.getPassengers = function () {
return this._passengers;
}
return Car;
})();
'use strict';
describe('Car class', function () {
it('should throw exception when no engine is specified', function () {
expect(Car).toThrowError('Engine type argument is required');
});
describe('drive method', function () {
var car;
beforeEach(function () {
car = new Car('V8');
});
it('should throw exception when no speed is provided', function () {
expect(function () { car.drive(); }).toThrowError(RangeError, 'Invalid driving speed');
});
it('should throw exception when speed type is invalid', function () {
expect(function () { car.drive('120'); }).toThrowError(RangeError, 'Invalid driving speed');
});
it('should throw exception when speed value is invalid', function () {
expect(car.drive.bind(null, -10)).toThrowError(RangeError, 'Invalid driving speed');
});
});
});