forked from michikono/typescript-tdd-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.ts
125 lines (107 loc) · 5.27 KB
/
driver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/// <reference path="../../references.ts" />
// When testing modules you can declare tests inside a module block
module Example {
class MockTransportationImpl implements MechanicalThings.Transportation {
constructor(private x, private y) {
}
sound() {
return 'mock';
}
move(offset:Coordinate) {
this.x = this.x + (offset.x * this.velocity());
this.y = this.y + (offset.y * this.velocity());
}
velocity() {
return 5;
}
position() {
return {x: this.x, y: this.y};
}
}
describe('Example Module', () => {
describe('Driver', () => {
describe('park()', () => {
it('(CUSTOM IMPL) should reset the transportation position to (0, 0)', () => {
var mockTransport = new MockTransportationImpl(2, 3);
var driver = new Driver(mockTransport);
driver.park();
assert.deepEqual({x: 0, y: 0}, mockTransport.position());
});
// see more on this topic here: http://sinonjs.org/docs/#mocks
// mocks are good for testing dependency objects are used correctly (looking for specific method calls)
it('(USING A MOCK) should reset the transportation position to (0, 0)', () => {
//var mockTransport = new MockTransportationImpl(2, 3);
var car = new MechanicalThings.CarImpl();
var driver = new Driver(car);
// register a mock
var mock = sandbox.mock(car);
mock.expects("position").once().returns({x: 0, y: 0});
mock.expects("move").withArgs({x: 0, y: 0}).once();
driver.park();
// verify that the expected behavior has executed
mock.verify();
});
// see more on this topic here: http://sinonjs.org/docs/#stubs
// stubs are best for specifying control flow; they overwrite behavior on specific methods
it('(USING A STUB) should reset the transportation position to (0, 0)', () => {
var mockTransport = new MechanicalThings.CarImpl();
// setup stubs to overwrite move method to force it to (0, 0)
var stub = sandbox.stub(mockTransport, 'move', function () {
this.x = 1;
this.y = 1;
});
var driver = new Driver(mockTransport);
driver.park();
// undo the stubs
stub.restore();
// should be parked at wherever we forced move to set the car
assert.deepEqual({x: 1, y: 1}, mockTransport.position());
});
// see more on this topic here: http://sinonjs.org/docs/#spies
// spies are best for callbacks, but should be avoided in favor of mocks for objects
// spies still execute the original methods!
it('(USING SPIES) should reset the transportation position to (0, 0)', () => {
var car = new MechanicalThings.CarImpl();
var positionSpy = sandbox.spy(car, "position");
var moveSpy = sandbox.spy(car, "move");
var driver = new Driver(car);
driver.park();
assert.ok(positionSpy.calledOnce);
assert.ok(moveSpy.withArgs({x: 0, y: 0}).calledOnce);
});
});
describe('goForward()', () => {
it('should change the position by (0, 1 * velocity)', () => {
var mockTransport = new MockTransportationImpl(2, 3);
var driver = new Driver(mockTransport);
driver.goForward();
assert.deepEqual({x: 2, y: 8}, mockTransport.position());
});
});
describe('goBackwards()', () => {
it('should change the position by (0, -1 * velocity)', () => {
var mockTransport = new MockTransportationImpl(2, 3);
var driver = new Driver(mockTransport);
driver.goBackwards();
assert.deepEqual({x: 2, y: -2}, mockTransport.position());
});
});
describe('turnLeft()', () => {
it('should change the position by (-1 * velocity, 0)', () => {
var mockTransport = new MockTransportationImpl(2, 3);
var driver = new Driver(mockTransport);
driver.turnLeft();
assert.deepEqual({x: -3, y: 3}, mockTransport.position());
});
});
describe('turnRight()', () => {
it('should change the position by (0, 1 * velocity)', () => {
var mockTransport = new MockTransportationImpl(2, 3);
var driver = new Driver(mockTransport);
driver.turnRight();
assert.deepEqual({x: 7, y: 3}, mockTransport.position());
});
});
});
});
}