Skip to content

Instantly share code, notes, and snippets.

@pawel-kaminski-krk
Created October 7, 2014 20:31
Show Gist options
  • Select an option

  • Save pawel-kaminski-krk/ce1f1310102fb6ef7841 to your computer and use it in GitHub Desktop.

Select an option

Save pawel-kaminski-krk/ce1f1310102fb6ef7841 to your computer and use it in GitHub Desktop.
test module
beforeEach(module(function($provide) {
"use strict";
$provide.service('_th', function($rootScope, $q) {
this.spyOnFn = function(service, name) {
spyOn(service, name).andCallThrough();
};
this.mockResponse = function(service, name, response) {
spyOn(service, name).andReturn(response);
};
this.deferResponse = function(service, name, defaultResponse) {
var deferred = $q.defer();
spyOn(service, name).andReturn(deferred.promise);
return {
resolve: function(response) {
deferred.resolve(response || defaultResponse);
$rootScope.$digest();
},
reject: function(rejected) {
deferred.reject(rejected || 'test woops.');
$rootScope.$digest();
}
};
};
})
}));
var responses = {
"data": [1,2,3]
};
angular
.module('appModule', [])
.controller('App', function($scope, Service) {
$scope.value = Service
.callServer()
.then(Server.process)
.catch(function() {
return 1;
})
})
.service('Service', function() {
this.callServer = function() {
return call.$promise;
};
this.process = function(data) {
return 4 + data.length;
};
this.isTrue = function() {
return true;
}
})
module.exports = function (config) {
config.set({
basePath: '../../',
frameworks: ["jasmine"],
files: [
'libs/jquery/dist/jquery.js',
'libs/angular/angular.js',
'libs/angular-loader/angular-loader.js',
'libs/angular-resource/angular-resource.js',
'libs/angular-sanitize/angular-sanitize.js',
'libs/angular-mocks/angular-mocks.js',
// src-es
'main/js/**/*.js',
//tests
_assertions.js
'test/js/**/*.js'
],
preprocessors: {
'**/*.html': ['ng-html2js']
},
autoWatch: true,
browsers: ['Chrome'],
port: 8090,
reporters: ['dots']
});
};
describe('spec:', function() {
'use strict';
beforeEach(module('appModule'));
var scope = null;
var initializeController;
beforeEach(inject(function ($injector, $rootScope, $controller) {
scope = $rootScope.$new();
initializeController = function() {
$controller('App', {$scope: scope});
};
}));
describe('initialize app:', function() {
it('pass.', inject(function(Service, _th) {
_th.spyOnFn(Service, 'process');
var csDfr = _th.deferResponse(Service, 'callServer', responses.data);
// when
initializeController();
// then
expect(scope.value).toBeUndefined();
// when
csDfr.resolve();
// then
expect(scope.value).toEqual(7);
}));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment