Skip to content

Commit a239198

Browse files
committed
Added jasmine example, added wrapper function around the mock
1 parent b22a088 commit a239198

3 files changed

Lines changed: 86 additions & 32 deletions

File tree

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,55 @@ define(["angular","ng-socket-io"],function(ng){
103103
})
104104
```
105105

106+
### Example test using Jasmine
107+
Test.js
108+
```js
109+
describe('Controller: socketController', function () {
110+
111+
// load the controller's module
112+
beforeEach(module('app'));
113+
114+
var socketController,
115+
notify,
116+
scope;
117+
118+
// Initialize the controller and a mock scope
119+
beforeEach(inject(function ($controller, $rootScope, _notify_) {
120+
scope = $rootScope.$new();
121+
notify = _notify_;
122+
socketController = $controller('socketController', {
123+
$scope: scope
124+
});
125+
126+
}));
127+
128+
it('The scope.items should change somehow', function() {
129+
expect(scope.items.length).toEqual(0);
130+
notify.receive('loadItems',{res: ['1','2','3']} )
131+
expect(scope.items.length).toEqual(3);
132+
});
133+
134+
});
135+
```
136+
137+
```js
138+
'use strict';
139+
140+
angular.module('app')
141+
.controller('socketController', function ($scope, notify) {
142+
// As first step to unit-test the socket, what we are
143+
// going to do it is to try mocking an error, and changing
144+
// a variable.
145+
// This I hope it's easy enough to get started. It is not easy.
146+
147+
148+
$scope.items = []
149+
notify.on("loadItems", function(res){
150+
$scope.items = res.res
151+
});
152+
});
153+
```
154+
106155
### Todo
107156

108157
* Usage with sinon.js for spies

angular-socket.io-mock.js

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,44 @@
11
/* global angular: false */
22
var ng = angular.module("btford.socket-io",[])
3-
ng.provider("socket",function(){
3+
ng.provider("socketFactory",function(){
44
this.$get = function($rootScope){
55

6-
var obj = {}
7-
obj.events = {}
8-
obj.emits = {}
9-
10-
// intercept 'on' calls and capture the callbacks
11-
obj.on = function(eventName, callback){
12-
if(!this.events[eventName]) this.events[eventName] = []
13-
this.events[eventName].push(callback)
14-
}
15-
16-
// intercept 'emit' calls from the client and record them to assert against in the test
17-
obj.emit = function(eventName){
18-
var args = Array.prototype.slice.call(arguments,1)
19-
20-
if(!this.emits[eventName])
21-
this.emits[eventName] = []
22-
this.emits[eventName].push(args)
23-
}
24-
25-
//simulate an inbound message to the socket from the server (only called from the test)
26-
obj.receive = function(eventName){
27-
var args = Array.prototype.slice.call(arguments,1)
28-
29-
if(this.events[eventName]){
30-
angular.forEach(this.events[eventName], function(callback){
31-
$rootScope.$apply(function() {
32-
callback.apply(this, args)
6+
return function() {
7+
var obj = {}
8+
obj.events = {}
9+
obj.emits = {}
10+
11+
// intercept 'on' calls and capture the callbacks
12+
obj.on = function(eventName, callback){
13+
if(!this.events[eventName]) this.events[eventName] = []
14+
this.events[eventName].push(callback)
15+
}
16+
17+
// intercept 'emit' calls from the client and record them to assert against in the test
18+
obj.emit = function(eventName){
19+
var args = Array.prototype.slice.call(arguments,1)
20+
21+
if(!this.emits[eventName])
22+
this.emits[eventName] = []
23+
this.emits[eventName].push(args)
24+
}
25+
26+
//simulate an inbound message to the socket from the server (only called from the test)
27+
obj.receive = function(eventName){
28+
var args = Array.prototype.slice.call(arguments,1)
29+
30+
if(this.events[eventName]){
31+
32+
angular.forEach(this.events[eventName], function(callback){
33+
$rootScope.$apply(function() {
34+
35+
callback.apply(this, args)
36+
})
3337
})
34-
})
38+
}
3539
}
36-
}
37-
return obj
40+
41+
return obj
42+
};
3843
}
3944
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"test"
2222
],
2323
"devDependencies": {
24-
"karma": "0.11.x"
24+
"karma": "^0.12.9"
2525
},
2626
"scripts": {
2727
"test": "karma start --single-run --browsers PhantomJS"

0 commit comments

Comments
 (0)