Skip to content

Commit f1b5b9a

Browse files
committed
style(tests): use consistent code style for route tests, clarify names
1 parent f0e568a commit f1b5b9a

File tree

3 files changed

+106
-106
lines changed

3 files changed

+106
-106
lines changed

Diff for: app/templates/server/api/thing/index.spec.js

+32-32
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,82 @@
22

33
var proxyquire = require('proxyquire').noPreserveCache();
44

5-
/* thing.controller stub */
6-
var thingCtrl = {
7-
index: 'thingCtrl.index'<% if(filters.mongoose) { %>,
8-
show: 'thingCtrl.show',
9-
create: 'thingCtrl.create',
10-
update: 'thingCtrl.update',
11-
destroy: 'thingCtrl.destroy'<% } %>
12-
},
13-
/* express.Router().router stub */
14-
router = {
15-
get: sinon.spy()<% if(filters.mongoose) { %>,
16-
put: sinon.spy(),
17-
patch: sinon.spy(),
18-
post: sinon.spy(),
19-
delete: sinon.spy()<% } %>
20-
},
21-
/* stubbed thing router */
22-
index = proxyquire('./index.js', {
23-
'express': {
24-
Router: function() {
25-
return router;
26-
}
27-
},
28-
'./thing.controller': thingCtrl
29-
});
5+
var thingCtrlStub = {
6+
index: 'thingCtrl.index'<% if(filters.mongoose) { %>,
7+
show: 'thingCtrl.show',
8+
create: 'thingCtrl.create',
9+
update: 'thingCtrl.update',
10+
destroy: 'thingCtrl.destroy'<% } %>
11+
};
12+
13+
var routerStub = {
14+
get: sinon.spy()<% if(filters.mongoose) { %>,
15+
put: sinon.spy(),
16+
patch: sinon.spy(),
17+
post: sinon.spy(),
18+
delete: sinon.spy()<% } %>
19+
};
20+
21+
// require the index with our stubbed out modules
22+
var thingIndex = proxyquire('./index.js', {
23+
'express': {
24+
Router: function() {
25+
return routerStub;
26+
}
27+
},
28+
'./thing.controller': thingCtrlStub
29+
});
3030

3131
describe('Thing API Router:', function() {
3232

3333
it('should return an express router instance', function() {
34-
index.should.equal(router);
34+
thingIndex.should.equal(routerStub);
3535
});
3636

3737
describe('GET /api/things', function() {
3838

3939
it('should route to thing.controller.index', function() {
40-
router.get.withArgs('/', 'thingCtrl.index').should.have.been.calledOnce;
40+
routerStub.get.withArgs('/', 'thingCtrl.index').should.have.been.calledOnce;
4141
});
4242

4343
});<% if(filters.mongoose) { %>
4444

4545
describe('GET /api/things/:id', function() {
4646

4747
it('should route to thing.controller.show', function() {
48-
router.get.withArgs('/:id', 'thingCtrl.show').should.have.been.calledOnce;
48+
routerStub.get.withArgs('/:id', 'thingCtrl.show').should.have.been.calledOnce;
4949
});
5050

5151
});
5252

5353
describe('POST /api/things', function() {
5454

5555
it('should route to thing.controller.create', function() {
56-
router.post.withArgs('/', 'thingCtrl.create').should.have.been.calledOnce;
56+
routerStub.post.withArgs('/', 'thingCtrl.create').should.have.been.calledOnce;
5757
});
5858

5959
});
6060

6161
describe('PUT /api/things/:id', function() {
6262

6363
it('should route to thing.controller.update', function() {
64-
router.put.withArgs('/:id', 'thingCtrl.update').should.have.been.calledOnce;
64+
routerStub.put.withArgs('/:id', 'thingCtrl.update').should.have.been.calledOnce;
6565
});
6666

6767
});
6868

6969
describe('PATCH /api/things/:id', function() {
7070

7171
it('should route to thing.controller.update', function() {
72-
router.patch.withArgs('/:id', 'thingCtrl.update').should.have.been.calledOnce;
72+
routerStub.patch.withArgs('/:id', 'thingCtrl.update').should.have.been.calledOnce;
7373
});
7474

7575
});
7676

7777
describe('DELETE /api/things/:id', function() {
7878

7979
it('should route to thing.controller.destroy', function() {
80-
router.delete.withArgs('/:id', 'thingCtrl.destroy').should.have.been.calledOnce;
80+
routerStub.delete.withArgs('/:id', 'thingCtrl.destroy').should.have.been.calledOnce;
8181
});
8282

8383
});<% } %>

Diff for: app/templates/server/api/user(auth)/index.spec.js

+42-42
Original file line numberDiff line numberDiff line change
@@ -2,92 +2,92 @@
22

33
var proxyquire = require('proxyquire').noPreserveCache();
44

5-
/* user.controller stub */
6-
var userCtrl = {
7-
index: 'userCtrl.index',
8-
destroy: 'userCtrl.destroy',
9-
me: 'userCtrl.me',
10-
changePassword: 'userCtrl.changePassword',
11-
show: 'userCtrl.show',
12-
create: 'userCtrl.create'
13-
},
14-
/* auth.service stub */
15-
authService = {
16-
isAuthenticated: function() {
17-
return 'authService.isAuthenticated';
18-
},
19-
hasRole: function(role) {
20-
return 'authService.hasRole.' + role;
21-
}
22-
},
23-
/* express.Router().router stub */
24-
router = {
25-
get: sinon.spy(),
26-
put: sinon.spy(),
27-
post: sinon.spy(),
28-
delete: sinon.spy()
29-
},
30-
/* stubbed user router */
31-
index = proxyquire('./index', {
32-
'express': {
33-
Router: function() {
34-
return router;
35-
}
36-
},
37-
'./user.controller': userCtrl,
38-
'../../auth/auth.service': authService
39-
});
5+
var userCtrlStub = {
6+
index: 'userCtrl.index',
7+
destroy: 'userCtrl.destroy',
8+
me: 'userCtrl.me',
9+
changePassword: 'userCtrl.changePassword',
10+
show: 'userCtrl.show',
11+
create: 'userCtrl.create'
12+
};
13+
14+
var authServiceStub = {
15+
isAuthenticated: function() {
16+
return 'authService.isAuthenticated';
17+
},
18+
hasRole: function(role) {
19+
return 'authService.hasRole.' + role;
20+
}
21+
};
22+
23+
var routerStub = {
24+
get: sinon.spy(),
25+
put: sinon.spy(),
26+
post: sinon.spy(),
27+
delete: sinon.spy()
28+
};
29+
30+
// require the index with our stubbed out modules
31+
var userIndex = proxyquire('./index', {
32+
'express': {
33+
Router: function() {
34+
return routerStub;
35+
}
36+
},
37+
'./user.controller': userCtrlStub,
38+
'../../auth/auth.service': authServiceStub
39+
});
4040

4141
describe('User API Router:', function() {
4242

4343
it('should return an express router instance', function() {
44-
index.should.equal(router);
44+
userIndex.should.equal(routerStub);
4545
});
4646

4747
describe('GET /api/users', function() {
4848

4949
it('should verify admin role and route to user.controller.index', function() {
50-
router.get.withArgs('/', 'authService.hasRole.admin', 'userCtrl.index').should.have.been.calledOnce;
50+
routerStub.get.withArgs('/', 'authService.hasRole.admin', 'userCtrl.index').should.have.been.calledOnce;
5151
});
5252

5353
});
5454

5555
describe('DELETE /api/users/:id', function() {
5656

5757
it('should verify admin role and route to user.controller.destroy', function() {
58-
router.delete.withArgs('/:id', 'authService.hasRole.admin', 'userCtrl.destroy').should.have.been.calledOnce;
58+
routerStub.delete.withArgs('/:id', 'authService.hasRole.admin', 'userCtrl.destroy').should.have.been.calledOnce;
5959
});
6060

6161
});
6262

6363
describe('GET /api/users/me', function() {
6464

6565
it('should be authenticated and route to user.controller.me', function() {
66-
router.get.withArgs('/me', 'authService.isAuthenticated', 'userCtrl.me').should.have.been.calledOnce;
66+
routerStub.get.withArgs('/me', 'authService.isAuthenticated', 'userCtrl.me').should.have.been.calledOnce;
6767
});
6868

6969
});
7070

7171
describe('PUT /api/users/:id/password', function() {
7272

7373
it('should be authenticated and route to user.controller.changePassword', function() {
74-
router.put.withArgs('/:id/password', 'authService.isAuthenticated', 'userCtrl.changePassword').should.have.been.calledOnce;
74+
routerStub.put.withArgs('/:id/password', 'authService.isAuthenticated', 'userCtrl.changePassword').should.have.been.calledOnce;
7575
});
7676

7777
});
7878

7979
describe('GET /api/users/:id', function() {
8080

8181
it('should be authenticated and route to user.controller.show', function() {
82-
router.get.withArgs('/:id', 'authService.isAuthenticated', 'userCtrl.show').should.have.been.calledOnce;
82+
routerStub.get.withArgs('/:id', 'authService.isAuthenticated', 'userCtrl.show').should.have.been.calledOnce;
8383
});
8484

8585
});
8686

8787
describe('POST /api/users', function() {
8888

8989
it('should route to user.controller.create', function() {
90-
router.post.withArgs('/', 'userCtrl.create').should.have.been.calledOnce;
90+
routerStub.post.withArgs('/', 'userCtrl.create').should.have.been.calledOnce;
9191
});
9292

9393
});

Diff for: endpoint/templates/index.spec.js

+32-32
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,82 @@
22

33
var proxyquire = require('proxyquire').noPreserveCache();
44

5-
/* <%= name %>.controller stub */
6-
var <%= cameledName %>Ctrl = {
7-
index: '<%= name %>Ctrl.index'<% if(filters.mongoose) { %>,
8-
show: '<%= name %>Ctrl.show',
9-
create: '<%= name %>Ctrl.create',
10-
update: '<%= name %>Ctrl.update',
11-
destroy: '<%= name %>Ctrl.destroy'<% } %>
12-
},
13-
/* express.Router().router stub */
14-
router = {
15-
get: sinon.spy()<% if(filters.mongoose) { %>,
16-
put: sinon.spy(),
17-
patch: sinon.spy(),
18-
post: sinon.spy(),
19-
delete: sinon.spy()<% } %>
20-
},
21-
/* stubbed <%= name %> router */
22-
index = proxyquire('./index.js', {
23-
'express': {
24-
Router: function() {
25-
return router;
26-
}
27-
},
28-
'./<%= name %>.controller': <%= cameledName %>Ctrl
29-
});
5+
var <%= cameledName %>CtrlStub = {
6+
index: '<%= name %>Ctrl.index'<% if(filters.mongoose) { %>,
7+
show: '<%= name %>Ctrl.show',
8+
create: '<%= name %>Ctrl.create',
9+
update: '<%= name %>Ctrl.update',
10+
destroy: '<%= name %>Ctrl.destroy'<% } %>
11+
};
12+
13+
var routerStub = {
14+
get: sinon.spy()<% if(filters.mongoose) { %>,
15+
put: sinon.spy(),
16+
patch: sinon.spy(),
17+
post: sinon.spy(),
18+
delete: sinon.spy()<% } %>
19+
};
20+
21+
// require the index with our stubbed out modules
22+
var <%= name %>Index = proxyquire('./index.js', {
23+
'express': {
24+
Router: function() {
25+
return routerStub;
26+
}
27+
},
28+
'./<%= name %>.controller': <%= cameledName %>CtrlStub
29+
});
3030

3131
describe('<%= classedName %> API Router:', function() {
3232

3333
it('should return an express router instance', function() {
34-
index.should.equal(router);
34+
<%= name %>Index.should.equal(router);
3535
});
3636

3737
describe('GET <%= route %>', function() {
3838

3939
it('should route to <%= name %>.controller.index', function() {
40-
router.get.withArgs('/', '<%= name %>Ctrl.index').should.have.been.calledOnce;
40+
routerStub.get.withArgs('/', '<%= name %>Ctrl.index').should.have.been.calledOnce;
4141
});
4242

4343
});<% if(filters.mongoose) { %>
4444

4545
describe('GET <%= route %>/:id', function() {
4646

4747
it('should route to <%= name %>.controller.show', function() {
48-
router.get.withArgs('/:id', '<%= name %>Ctrl.show').should.have.been.calledOnce;
48+
routerStub.get.withArgs('/:id', '<%= name %>Ctrl.show').should.have.been.calledOnce;
4949
});
5050

5151
});
5252

5353
describe('POST <%= route %>', function() {
5454

5555
it('should route to <%= name %>.controller.create', function() {
56-
router.post.withArgs('/', '<%= name %>Ctrl.create').should.have.been.calledOnce;
56+
routerStub.post.withArgs('/', '<%= name %>Ctrl.create').should.have.been.calledOnce;
5757
});
5858

5959
});
6060

6161
describe('PUT <%= route %>/:id', function() {
6262

6363
it('should route to <%= name %>.controller.update', function() {
64-
router.put.withArgs('/:id', '<%= name %>Ctrl.update').should.have.been.calledOnce;
64+
routerStub.put.withArgs('/:id', '<%= name %>Ctrl.update').should.have.been.calledOnce;
6565
});
6666

6767
});
6868

6969
describe('PATCH <%= route %>/:id', function() {
7070

7171
it('should route to <%= name %>.controller.update', function() {
72-
router.patch.withArgs('/:id', '<%= name %>Ctrl.update').should.have.been.calledOnce;
72+
routerStub.patch.withArgs('/:id', '<%= name %>Ctrl.update').should.have.been.calledOnce;
7373
});
7474

7575
});
7676

7777
describe('DELETE <%= route %>/:id', function() {
7878

7979
it('should route to <%= name %>.controller.destroy', function() {
80-
router.delete.withArgs('/:id', '<%= name %>Ctrl.destroy').should.have.been.calledOnce;
80+
routerStub.delete.withArgs('/:id', '<%= name %>Ctrl.destroy').should.have.been.calledOnce;
8181
});
8282

8383
});<% } %>

0 commit comments

Comments
 (0)