Skip to content

Commit 7bc7acc

Browse files
committed
updated unit tests for controller to use $http
1 parent 37b4177 commit 7bc7acc

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

Diff for: templates/coffeescript-min/spec/controller.coffee

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ describe 'Controller: <%= _.classify(name) %>Ctrl', () ->
77

88
<%= _.classify(name) %>Ctrl = {}
99
scope = {}
10+
$httpBackend = {}
1011

1112
# Initialize the controller and a mock scope
12-
beforeEach inject ($controller, $rootScope) ->
13+
beforeEach inject (_$httpBackend_, $controller, $rootScope) ->
14+
$httpBackend = _$httpBackend_
15+
$httpBackend.expectGET('/api/awesomeThings').respond ['HTML5 Boilerplate', 'AngularJS', 'Karma', 'Express']
1316
scope = $rootScope.$new()
1417
<%= _.classify(name) %>Ctrl = $controller '<%= _.classify(name) %>Ctrl', {
1518
$scope: scope
1619
}
1720

1821
it 'should attach a list of awesomeThings to the scope', () ->
19-
expect(scope.awesomeThings.length).toBe 3
22+
expect(scope.awesomeThings).toBeUndefined()
23+
$httpBackend.flush()
24+
expect(scope.awesomeThings.length).toBe 4

Diff for: templates/coffeescript/spec/controller.coffee

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ describe 'Controller: <%= _.classify(name) %>Ctrl', () ->
77

88
<%= _.classify(name) %>Ctrl = {}
99
scope = {}
10+
$httpBackend = {}
1011

1112
# Initialize the controller and a mock scope
12-
beforeEach inject ($controller, $rootScope) ->
13+
beforeEach inject (_$httpBackend_, $controller, $rootScope) ->
14+
$httpBackend = _$httpBackend_
15+
$httpBackend.expectGET('/api/awesomeThings').respond ['HTML5 Boilerplate', 'AngularJS', 'Karma', 'Express']
1316
scope = $rootScope.$new()
1417
<%= _.classify(name) %>Ctrl = $controller '<%= _.classify(name) %>Ctrl', {
1518
$scope: scope
1619
}
1720

1821
it 'should attach a list of awesomeThings to the scope', () ->
19-
expect(scope.awesomeThings.length).toBe 3
22+
expect(scope.awesomeThings).toBeUndefined()
23+
$httpBackend.flush()
24+
expect(scope.awesomeThings.length).toBe 4

Diff for: templates/javascript-min/spec/controller.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@ describe('Controller: <%= _.classify(name) %>Ctrl', function () {
66
beforeEach(module('<%= _.camelize(appname) %>App'));
77

88
var <%= _.classify(name) %>Ctrl,
9-
scope;
9+
scope,
10+
$httpBackend;
1011

1112
// Initialize the controller and a mock scope
12-
beforeEach(inject(function ($controller, $rootScope) {
13+
beforeEach(inject(function (_$httpBackend_, $controller, $rootScope) {
14+
$httpBackend = _$httpBackend_;
15+
$httpBackend.expectGET('/api/awesomeThings')
16+
.respond(['HTML5 Boilerplate', 'AngularJS', 'Karma', 'Express']);
1317
scope = $rootScope.$new();
1418
<%= _.classify(name) %>Ctrl = $controller('<%= _.classify(name) %>Ctrl', {
1519
$scope: scope
1620
});
1721
}));
1822

1923
it('should attach a list of awesomeThings to the scope', function () {
20-
expect(scope.awesomeThings.length).toBe(3);
24+
expect(scope.awesomeThings).toBeUndefined();
25+
$httpBackend.flush();
26+
expect(scope.awesomeThings.length).toBe(4);
2127
});
2228
});

Diff for: templates/javascript/spec/controller.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@ describe('Controller: <%= _.classify(name) %>Ctrl', function () {
66
beforeEach(module('<%= _.camelize(appname) %>App'));
77

88
var <%= _.classify(name) %>Ctrl,
9-
scope;
9+
scope,
10+
$httpBackend;
1011

1112
// Initialize the controller and a mock scope
12-
beforeEach(inject(function ($controller, $rootScope) {
13+
beforeEach(inject(function (_$httpBackend_, $controller, $rootScope) {
14+
$httpBackend = _$httpBackend_;
15+
$httpBackend.expectGET('/api/awesomeThings')
16+
.respond(['HTML5 Boilerplate', 'AngularJS', 'Karma', 'Express']);
1317
scope = $rootScope.$new();
1418
<%= _.classify(name) %>Ctrl = $controller('<%= _.classify(name) %>Ctrl', {
1519
$scope: scope
1620
});
1721
}));
1822

1923
it('should attach a list of awesomeThings to the scope', function () {
20-
expect(scope.awesomeThings.length).toBe(3);
24+
expect(scope.awesomeThings).toBeUndefined();
25+
$httpBackend.flush();
26+
expect(scope.awesomeThings.length).toBe(4);
2127
});
2228
});

0 commit comments

Comments
 (0)