|
1 | 1 | angular-socket.io-mock |
2 | 2 | ====================== |
3 | 3 |
|
4 | | -Mock Socket.io bindings for AngularJS, useful for testing as a drop in replacement |
| 4 | +Mock Socket.io bindings for AngularJS, useful for testing as a drop in replacement for btford/angular-socket-io |
| 5 | +which allows testing against a fake server. |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +### Standard usage |
| 10 | + |
| 11 | +* Make sure AngularJS is loaded prior to inclusion. |
| 12 | + |
| 13 | +karma.conf.js |
| 14 | +```js |
| 15 | +module.exports = function(config) { |
| 16 | + config.set({ |
| 17 | + files: [ |
| 18 | + {pattern: "bower_components/angular-socket.io-mock.js", included: false} |
| 19 | + ] |
| 20 | + }) |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +## With RequireJS |
| 25 | + |
| 26 | +In all our projects we utilize RequireJS to use this test library with RequireJS is very simple. Just replace the entry |
| 27 | +that points to the standard angular-socket-io with this library. Below is an example. |
| 28 | + |
| 29 | +main.js |
| 30 | +```js |
| 31 | +require.config({ |
| 32 | + baseUrl: "/base/js", |
| 33 | + shim: { |
| 34 | + "socketio": {exports: "io"}, |
| 35 | + "angular": {exports: "angular"}, |
| 36 | + "ng-socket-io": ["angular","socketio"], |
| 37 | + "ng-mocks": ["angular"] |
| 38 | + }, |
| 39 | + paths: { |
| 40 | + "angular": "/base/bower_components/angular/angular", |
| 41 | + "socketio": "/base/bower_components/socket.io-client/dist/socket.io", |
| 42 | + "ng-socket-io": "/base/bower_components/angular-socket.io-mock/angular-socket.io-mock", |
| 43 | + "ng-mocks": "/base/bower_components/angular-mocks/angular-mocks" |
| 44 | + }, |
| 45 | + callback: window.__karma__.start |
| 46 | +}) |
| 47 | +``` |
| 48 | + |
| 49 | +Your bower.json should look something like this. |
| 50 | + |
| 51 | +bower.json |
| 52 | +```json |
| 53 | +{ |
| 54 | + "name": "my-app", |
| 55 | + "version": "0.0.1", |
| 56 | + "dependencies": { |
| 57 | + "angular": "latest", |
| 58 | + "angular-socket-io": "latest" |
| 59 | + }, |
| 60 | + "devDependencies": { |
| 61 | + "socket.io-client": "latest", |
| 62 | + "angular-mocks": "latest", |
| 63 | + "angular-socket.io-mock": "latest" |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### Example Test |
| 69 | + |
| 70 | +This example uses Mocha Test as the testing framework and ChaiJS for the assertion library. It also uses RequireJS to |
| 71 | +load the environment. |
| 72 | + |
| 73 | +test.js |
| 74 | +```js |
| 75 | +define(["chai","angular","angular-mocks","angular-socket-io"],function(chai){ |
| 76 | + describe("Test Suite",function(){ |
| 77 | + beforeEach(module("app")) |
| 78 | + it("should be using the mock ng-socket.io",$inject(function($rootScope,$controller){ |
| 79 | + var expect = chai.expect |
| 80 | + , scope = $rootScope.$new() |
| 81 | + , ctrl = $controller("MyController",{$scope: scope}) |
| 82 | + expect(scope.save).to.be.a("function") |
| 83 | + expect(scope.items).to.be.a("array") |
| 84 | + expect(scope.items.length).to.equal(3) |
| 85 | + })) |
| 86 | + }) |
| 87 | +}) |
| 88 | +``` |
| 89 | + |
| 90 | +MyController.js |
| 91 | +```js |
| 92 | +define(["angular","ng-socket-io"],function(ng){ |
| 93 | + var app = ng.module("app",["btford.socket-io"]) |
| 94 | + app.controller("MyController",["$scope","socket",function($scope,socket){ |
| 95 | + //load items now |
| 96 | + $scope.items = [] |
| 97 | + socket.emit("loadItems",function({},function(res){ |
| 98 | + $scope.items = res.res |
| 99 | + }) |
| 100 | + //setup functions |
| 101 | + $scope.save = function(){} |
| 102 | + }]) |
| 103 | +}) |
| 104 | +``` |
| 105 | +
|
| 106 | +*Note* Please see https://github.com/btford/angular-socket-io for library usage. |
0 commit comments