Skip to content

Commit 11ce5c1

Browse files
committed
feat(gen): replace does templating syntax
Changes: * Use `<%= expect() %>ASSERTION_SUBJECT<%= to() %>` syntax * Implement dynamic assertions for all server tests including endpoints * Default to `should` even when jasmine is chosen since there are no jasmine server tests * Refactor tests to comply with project indentation closes #1163
1 parent 04daafa commit 11ce5c1

15 files changed

+109
-115
lines changed

Diff for: app/index.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,12 @@ var AngularFullstackGenerator = yeoman.generators.Base.extend({
2626

2727
this.filters = {};
2828

29-
// dynamic assertion statement
30-
this.does = this.is = function(foo) {
31-
foo = this.engine(foo.replace(/\(;>%%<;\)/g, '<%')
32-
.replace(/\(;>%<;\)/g, '%>'), this);
33-
if (this.filters.should) {
34-
return foo + '.should';
35-
} else {
36-
return 'expect(' + foo + ').to';
37-
}
29+
// dynamic assertion statements
30+
this.expect = function() {
31+
return this.filters.expect ? 'expect(' : '';
32+
}.bind(this);
33+
this.to = function() {
34+
return this.filters.expect ? ').to' : '.should';
3835
}.bind(this);
3936
},
4037

Diff for: app/templates/client/app/main/main.controller.spec(coffee).coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ describe 'Controller: MainCtrl', ->
2929
it 'should attach a list of things to the scope', ->
3030
$httpBackend.flush()<% if (filters.jasmine) { %>
3131
expect(scope.awesomeThings.length).toBe 4 <% } if (filters.mocha) { %>
32-
<%= does("scope.awesomeThings.length") %>.equal 4<% } %>
32+
<%= expect() %>scope.awesomeThings.length<%= to() %>.equal 4<% } %>

Diff for: app/templates/client/app/main/main.controller.spec(js).js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ describe('Controller: MainCtrl', function() {
2828
it('should attach a list of things to the scope', function() {
2929
$httpBackend.flush();<% if (filters.jasmine) { %>
3030
expect(scope.awesomeThings.length).toBe(4);<% } if (filters.mocha) { %>
31-
<%= does("scope.awesomeThings.length") %>.equal(4);<% } %>
31+
<%= expect() %>scope.awesomeThings.length<%= to() %>.equal(4);<% } %>
3232
});
3333
});

Diff for: app/templates/client/components/auth(auth)/user.service(js).js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ angular.module('<%= scriptAppName %>')
1818
id:'me'
1919
}
2020
}
21-
});
21+
});
2222
});

Diff for: app/templates/e2e/account(auth)/login/login.spec(mocha).js

+10-10
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ describe('Login View', function() {
3535
});
3636

3737
it('should include login form with correct inputs and submit button', function() {
38-
<%= does("page.form.email.getAttribute('type')") %>.eventually.equal('email');
39-
<%= does("page.form.email.getAttribute('name')") %>.eventually.equal('email');
40-
<%= does("page.form.password.getAttribute('type')") %>.eventually.equal('password');
41-
<%= does("page.form.password.getAttribute('name')") %>.eventually.equal('password');
42-
<%= does("page.form.submit.getAttribute('type')") %>.eventually.equal('submit');
43-
<%= does("page.form.submit.getText()") %>.eventually.equal('Login');
38+
<%= expect() %>page.form.email.getAttribute('type')<%= to() %>.eventually.equal('email');
39+
<%= expect() %>page.form.email.getAttribute('name')<%= to() %>.eventually.equal('email');
40+
<%= expect() %>page.form.password.getAttribute('type')<%= to() %>.eventually.equal('password');
41+
<%= expect() %>page.form.password.getAttribute('name')<%= to() %>.eventually.equal('password');
42+
<%= expect() %>page.form.submit.getAttribute('type')<%= to() %>.eventually.equal('submit');
43+
<%= expect() %>page.form.submit.getText()<%= to() %>.eventually.equal('Login');
4444
});
4545

4646
describe('with local auth', function() {
@@ -50,8 +50,8 @@ describe('Login View', function() {
5050

5151
var navbar = require('../../components/navbar/navbar.po');
5252

53-
<%= does("browser.getCurrentUrl()") %>.eventually.equal(config.baseUrl + '/');
54-
<%= does("navbar.navbarAccountGreeting.getText()") %>.eventually.equal('Hello ' + testUser.name);
53+
<%= expect() %>browser.getCurrentUrl()<%= to() %>.eventually.equal(config.baseUrl + '/');
54+
<%= expect() %>navbar.navbarAccountGreeting.getText()<%= to() %>.eventually.equal('Hello ' + testUser.name);
5555
});
5656

5757
describe('and invalid credentials', function() {
@@ -65,10 +65,10 @@ describe('Login View', function() {
6565
password: 'badPassword'
6666
});
6767

68-
<%= does("browser.getCurrentUrl()") %>.eventually.equal(config.baseUrl + '/login');
68+
<%= expect() %>browser.getCurrentUrl()<%= to() %>.eventually.equal(config.baseUrl + '/login');
6969

7070
var helpBlock = page.form.element(by.css('.form-group.has-error .help-block.ng-binding'));
71-
<%= does("helpBlock.getText()") %>.eventually.equal('This password is not correct.');
71+
<%= expect() %>helpBlock.getText()<%= to() %>.eventually.equal('This password is not correct.');
7272
});
7373

7474
});

Diff for: app/templates/e2e/account(auth)/logout/logout.spec(mocha).js

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ describe('Logout View', function() {
3939
it('should logout a user and redirecting to "/"', function() {
4040
var navbar = require('../../components/navbar/navbar.po');
4141

42-
<%= does("browser.getCurrentUrl()") %>.eventually.equal(config.baseUrl + '/');
43-
<%= does("navbar.navbarAccountGreeting.getText()") %>.eventually.equal('Hello ' + testUser.name);
42+
<%= expect() %>browser.getCurrentUrl()<%= to() %>.eventually.equal(config.baseUrl + '/');
43+
<%= expect() %>navbar.navbarAccountGreeting.getText()<%= to() %>.eventually.equal('Hello ' + testUser.name);
4444

4545
browser.get(config.baseUrl + '/logout');
4646

4747
navbar = require('../../components/navbar/navbar.po');
4848

49-
<%= does("browser.getCurrentUrl()") %>.eventually.equal(config.baseUrl + '/');
50-
<%= does("navbar.navbarAccountGreeting.isDisplayed()") %>.eventually.equal(false);
49+
<%= expect() %>browser.getCurrentUrl()<%= to() %>.eventually.equal(config.baseUrl + '/');
50+
<%= expect() %>navbar.navbarAccountGreeting.isDisplayed()<%= to() %>.eventually.equal(false);
5151
});
5252

5353
});

Diff for: app/templates/e2e/account(auth)/signup/signup.spec(mocha).js

+13-13
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ describe('Signup View', function() {
2929
});
3030

3131
it('should include signup form with correct inputs and submit button', function() {
32-
<%= does("page.form.name.getAttribute('type')") %>.eventually.equal('text');
33-
<%= does("page.form.name.getAttribute('name')") %>.eventually.equal('name');
34-
<%= does("page.form.email.getAttribute('type')") %>.eventually.equal('email');
35-
<%= does("page.form.email.getAttribute('name')") %>.eventually.equal('email');
36-
<%= does("page.form.password.getAttribute('type')") %>.eventually.equal('password');
37-
<%= does("page.form.password.getAttribute('name')") %>.eventually.equal('password');
38-
<%= does("page.form.submit.getAttribute('type')") %>.eventually.equal('submit');
39-
<%= does("page.form.submit.getText()") %>.eventually.equal('Sign up');
32+
<%= expect() %>page.form.name.getAttribute('type')<%= to() %>.eventually.equal('text');
33+
<%= expect() %>page.form.name.getAttribute('name')<%= to() %>.eventually.equal('name');
34+
<%= expect() %>page.form.email.getAttribute('type')<%= to() %>.eventually.equal('email');
35+
<%= expect() %>page.form.email.getAttribute('name')<%= to() %>.eventually.equal('email');
36+
<%= expect() %>page.form.password.getAttribute('type')<%= to() %>.eventually.equal('password');
37+
<%= expect() %>page.form.password.getAttribute('name')<%= to() %>.eventually.equal('password');
38+
<%= expect() %>page.form.submit.getAttribute('type')<%= to() %>.eventually.equal('submit');
39+
<%= expect() %>page.form.submit.getText()<%= to() %>.eventually.equal('Sign up');
4040
});
4141

4242
describe('with local auth', function() {
@@ -51,8 +51,8 @@ describe('Signup View', function() {
5151

5252
var navbar = require('../../components/navbar/navbar.po');
5353

54-
<%= does("browser.getCurrentUrl()") %>.eventually.equal(config.baseUrl + '/');
55-
<%= does("navbar.navbarAccountGreeting.getText()") %>.eventually.equal('Hello ' + testUser.name);
54+
<%= expect() %>browser.getCurrentUrl()<%= to() %>.eventually.equal(config.baseUrl + '/');
55+
<%= expect() %>navbar.navbarAccountGreeting.getText()<%= to() %>.eventually.equal('Hello ' + testUser.name);
5656
});
5757

5858
describe('and invalid credentials', function() {
@@ -63,11 +63,11 @@ describe('Signup View', function() {
6363
it('should indicate signup failures', function() {
6464
page.signup(testUser);
6565

66-
<%= does("browser.getCurrentUrl()") %>.eventually.equal(config.baseUrl + '/signup');
67-
<%= does("page.form.email.getAttribute('class')") %>.eventually.contain('ng-invalid-mongoose');
66+
<%= expect() %>browser.getCurrentUrl()<%= to() %>.eventually.equal(config.baseUrl + '/signup');
67+
<%= expect() %>page.form.email.getAttribute('class')<%= to() %>.eventually.contain('ng-invalid-mongoose');
6868

6969
var helpBlock = page.form.element(by.css('.form-group.has-error .help-block.ng-binding'));
70-
<%= does("helpBlock.getText()") %>.eventually.equal('The specified email address is already in use.');
70+
<%= expect() %>helpBlock.getText()<%= to() %>.eventually.equal('The specified email address is already in use.');
7171
});
7272

7373
});

Diff for: app/templates/e2e/main/main.spec(mocha).js

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ describe('Main View', function() {
1111
});
1212

1313
it('should include jumbotron with correct data', function() {
14-
<%= does("page.h1El.getText()") %>.eventually.equal('\'Allo, \'Allo!');
15-
<%= does("page.imgEl.getAttribute('src')") %>.eventually.match(/yeoman.png$/);
16-
<%= does("page.imgEl.getAttribute('alt')") %>.eventually.equal('I\'m Yeoman');
14+
<%= expect() %>page.h1El.getText()<%= to() %>.eventually.equal('\'Allo, \'Allo!');
15+
<%= expect() %>page.imgEl.getAttribute('src')<%= to() %>.eventually.match(/yeoman.png$/);
16+
<%= expect() %>page.imgEl.getAttribute('alt')<%= to() %>.eventually.equal('I\'m Yeoman');
1717
});
1818
});

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

+19-19
Original file line numberDiff line numberDiff line change
@@ -41,65 +41,65 @@ var userIndex = proxyquire('./index', {
4141
describe('User API Router:', function() {
4242

4343
it('should return an express router instance', function() {
44-
userIndex.should.equal(routerStub);
44+
<%= expect() %>userIndex<%= to() %>.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-
routerStub.get
51-
.withArgs('/', 'authService.hasRole.admin', 'userCtrl.index')
52-
.should.have.been.calledOnce;
50+
<%= expect() %>routerStub.get
51+
.withArgs('/', 'authService.hasRole.admin', 'userCtrl.index')
52+
<%= to() %>.have.been.calledOnce;
5353
});
5454

5555
});
5656

5757
describe('DELETE /api/users/:id', function() {
5858

5959
it('should verify admin role and route to user.controller.destroy', function() {
60-
routerStub.delete
61-
.withArgs('/:id', 'authService.hasRole.admin', 'userCtrl.destroy')
62-
.should.have.been.calledOnce;
60+
<%= expect() %>routerStub.delete
61+
.withArgs('/:id', 'authService.hasRole.admin', 'userCtrl.destroy')
62+
<%= to() %>.have.been.calledOnce;
6363
});
6464

6565
});
6666

6767
describe('GET /api/users/me', function() {
6868

6969
it('should be authenticated and route to user.controller.me', function() {
70-
routerStub.get
71-
.withArgs('/me', 'authService.isAuthenticated', 'userCtrl.me')
72-
.should.have.been.calledOnce;
70+
<%= expect() %>routerStub.get
71+
.withArgs('/me', 'authService.isAuthenticated', 'userCtrl.me')
72+
<%= to() %>.have.been.calledOnce;
7373
});
7474

7575
});
7676

7777
describe('PUT /api/users/:id/password', function() {
7878

7979
it('should be authenticated and route to user.controller.changePassword', function() {
80-
routerStub.put
81-
.withArgs('/:id/password', 'authService.isAuthenticated', 'userCtrl.changePassword')
82-
.should.have.been.calledOnce;
80+
<%= expect() %>routerStub.put
81+
.withArgs('/:id/password', 'authService.isAuthenticated', 'userCtrl.changePassword')
82+
<%= to() %>.have.been.calledOnce;
8383
});
8484

8585
});
8686

8787
describe('GET /api/users/:id', function() {
8888

8989
it('should be authenticated and route to user.controller.show', function() {
90-
routerStub.get
91-
.withArgs('/:id', 'authService.isAuthenticated', 'userCtrl.show')
92-
.should.have.been.calledOnce;
90+
<%= expect() %>routerStub.get
91+
.withArgs('/:id', 'authService.isAuthenticated', 'userCtrl.show')
92+
<%= to() %>.have.been.calledOnce;
9393
});
9494

9595
});
9696

9797
describe('POST /api/users', function() {
9898

9999
it('should route to user.controller.create', function() {
100-
routerStub.post
101-
.withArgs('/', 'userCtrl.create')
102-
.should.have.been.calledOnce;
100+
<%= expect() %>routerStub.post
101+
.withArgs('/', 'userCtrl.create')
102+
<%= to() %>.have.been.calledOnce;
103103
});
104104

105105
});

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('User API:', function() {
5555
.expect(200)
5656
.expect('Content-Type', /json/)
5757
.end(function(err, res) {
58-
res.body._id.toString().should.equal(user._id.toString());
58+
<%= expect() %>res.body._id.toString()<%= to() %>.equal(user._id.toString());
5959
done();
6060
});
6161
});

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@ describe('User Model', function() {
2828
});
2929

3030
it('should begin with no users', function() {
31-
return User.findAsync({})
32-
.should.eventually.have.length(0);
31+
return <%= expect() %>User.findAsync({})<%= to() %>
32+
.eventually.have.length(0);
3333
});
3434

3535
it('should fail when saving a duplicate user', function() {
36-
return user.saveAsync()
36+
return <%= expect() %>user.saveAsync()
3737
.then(function() {
3838
var userDup = genUser();
3939
return userDup.saveAsync();
40-
}).should.be.rejected;
40+
})<%= to() %>.be.rejected;
4141
});
4242

4343
describe('#email', function() {
4444
it('should fail when saving without an email', function() {
4545
user.email = '';
46-
return user.saveAsync().should.be.rejected;
46+
return <%= expect() %>user.saveAsync()<%= to() %>.be.rejected;
4747
});
4848
});
4949

@@ -53,19 +53,19 @@ describe('User Model', function() {
5353
});
5454

5555
it('should authenticate user if valid', function() {
56-
user.authenticate('password').should.be.true;
56+
<%= expect() %>user.authenticate('password')<%= to() %>.be.true;
5757
});
5858

5959
it('should not authenticate user if invalid', function() {
60-
user.authenticate('blah').should.not.be.true;
60+
<%= expect() %>user.authenticate('blah')<%= to() %>.not.be.true;
6161
});
6262

6363
it('should remain the same hash unless the password is updated', function() {
6464
user.name = 'Test User';
65-
return user.saveAsync()
65+
return <%= expect() %>user.saveAsync()
6666
.spread(function(u) {
6767
return u.authenticate('password');
68-
}).should.eventually.be.true;
68+
})<%= to() %>.eventually.be.true;
6969
});
7070
});
7171

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,22 @@ describe('User Model', function() {
3030
});
3131

3232
it('should begin with no users', function() {
33-
return User.findAll()
34-
.should.eventually.have.length(0);
33+
return <%= expect() %>User.findAll()<%= to() %>
34+
.eventually.have.length(0);
3535
});
3636

3737
it('should fail when saving a duplicate user', function() {
38-
return user.save()
38+
return <%= expect() %>user.save()
3939
.then(function() {
4040
var userDup = genUser();
4141
return userDup.save();
42-
}).should.be.rejected;
42+
})<%= to() %>.be.rejected;
4343
});
4444

4545
describe('#email', function() {
4646
it('should fail when saving without an email', function() {
4747
user.email = '';
48-
return user.save().should.be.rejected;
48+
return <%= expect() %>user.save()<%= to() %>.be.rejected;
4949
});
5050
});
5151

@@ -55,19 +55,19 @@ describe('User Model', function() {
5555
});
5656

5757
it('should authenticate user if valid', function() {
58-
user.authenticate('password').should.be.true;
58+
<%= expect() %>user.authenticate('password')<%= to() %>.be.true;
5959
});
6060

6161
it('should not authenticate user if invalid', function() {
62-
user.authenticate('blah').should.not.be.true;
62+
<%= expect() %>user.authenticate('blah')<%= to() %>.not.be.true;
6363
});
6464

6565
it('should remain the same hash unless the password is updated', function() {
6666
user.name = 'Test User';
67-
return user.save()
67+
return <%= expect() %>user.save()
6868
.then(function(u) {
6969
return u.authenticate('password');
70-
}).should.eventually.be.true;
70+
})<%= to() %>.eventually.be.true;
7171
});
7272
});
7373

0 commit comments

Comments
 (0)