Sharing Top Content from the Angular-sphere.

Angularjs Factory Unit Test Case – AngularJs beginner guide

Angularjs Factory Unit Test Case

  • getList(); – } – – return model; – }); – – return Users; – } – })(); – – Unit test case of factory – – describe(‘Factories Resources’, function() { – describe(‘Users’, function() { – beforeEach(module(‘myApp’)); – – var $httpBackend, Users, apiUrl; – – { – Users = $injector.get(‘Users’); -…
  • respond([ – { “id”: 1, “name”: “user1” }, – { “id”: 2, “name”: “user2” }, – { “id”: 3, “name”: “user3” }, – { “id”: 4, “name”: “user4” } – ]); – – { – }); – – $httpBackend.flush(); – }); – }); – – describe(‘Show’, function() { – it(“queries…
  • png when avatarURl is null and gender is ‘F'”, function() { – { – }); – }); – }); – – describe(‘isOnline ‘, function() { – beforeEach(function() { – var data = [ – { “id”: 1, “user_name”: “user1”, “updated_at”: moment().
  • respond(data); – }); – – it(“returns true when updated_at less than 5 minutes ago”, function() { – { – }); – – $httpBackend.flush(); – }); – – it(“returns false when updated_at more than 5 minutes ago”, function() { – { – }); – – $httpBackend.flush(); – }); – }); -…
  • respond(data); – }); – – it(“returns true when created_at less than 48 hours ago”, function() { – { – }); – $httpBackend.flush(); – }); – – it(“returns false when created_at more than 48 hours ago”, function() { – { – }); – $httpBackend.flush(); – – }); – }); – -…

Factory Code (function() { ‘use strict’; angular .module(‘myApp.restangular’) .factory(‘Users’, Users); Users.$inject = [ ‘Restangular’, ‘apiUrl’ ]; function Users(Restangular, apiUrl) { var Users = Restangular.service(‘users’); Restangular.extendModel(‘users’, function(model) { model.isRecent = function() { var createdAt = moment.utc(this.created_at); var difference = moment.duration(moment.utc().diff(createdAt)); return difference.asHours() < 48; } model.lastConnection = function() { return moment.utc(this.updated_at).fromNow(); }; model.isOnline = function() {…

Factory Code

(function() {

‘use strict’;

angular

.module(‘myApp.restangular’)

.factory(‘Users’, Users);

Users.$inject = [ ‘Restangular’, ‘apiUrl’ ];

function Users(Restangular, apiUrl) {

var Users = function(model) {

model.isRecent = function() {

var createdAt = moment.utc(this.created_at);

var difference = return difference.asHours() 48;

}

model.lastConnection = function() {

return };

model.isOnline = function() {

var updatedAt = moment.utc(this.updated_at);

var difference = return difference.asMinutes() 5;

};

model.avatarPlaceholder = function() {

return (this.gender == “M” ? : };

model.avatarSrc = function() {

return (this.avatar_url == null ? this.avatarPlaceholder() : apiUrl + this.avatar_url);

};

model.getHobbies = function() {

return Restangular.service(‘hobbies’, Restangular.one(‘users’, this.id)).getList();

};

model.getPassions = function() {

return Restangular.one(‘users’, this.id)).getList();

}

model.getFriends = function() {

return Restangular.service(‘friends’, Restangular.one(‘users’, this.id)).getList();

}

return model;

});

return Users;

}

})();

Unit test case of factory

Angularjs Factory Unit Test Case – AngularJs beginner guide