Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
JS Bin [Testing AngularJS 1.x Services that use $resource with Jasmine] // source https://jsbin.com/qiniyow
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="[Testing AngularJS 1.x Services that use $resource with Jasmine]">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script type="text/javascript" src="https://jeromecovington.github.io/jasmine-all/jasmine-all.js"></script>
<script src="https://code.angularjs.org/1.6.4/angular.js"></script>
<script src="https://code.angularjs.org/1.6.4/angular-resource.js"></script>
<script src="https://code.angularjs.org/1.6.4/angular-mocks.js"></script>
</head>
<body>
<script id="jsbin-javascript">
//--- CODE --------------------------
(function (angular) {
var myApp = angular.module('myApp', ['ngResource']);
// Original Service
myApp.service('Original', function () {
var self = this;
this.saidHello = false;
this.sayHelloSuccess = function () {
self.saidHello = true;
console.log('success!');
};
this.sayHelloFailure = function (error) {
self.saidHello = false;
console.log('failure: ' + error);
};
});
// Original Resource Service
myApp.service('OriginalResource', function ($resource, Original) {
var resource = $resource('/say-hello');
this.sayHello = function (name) {
return resource.get({}, { name: name }).$promise.then(
function (response) {
Original.sayHelloSuccess();
},
function (error) {
Original.sayHelloFailure(error);
}
);
}
});
})(angular);
// ---SPECS-------------------------
describe('myApp', function () {
beforeEach(function () {
module('myApp');
});
// Original.spec
describe('service: original', function () {
var Original;
beforeEach(inject(function (_Original_) {
Original = _Original_;
}));
it('says hello', function() {
Original.sayHelloSuccess();
expect(Original.saidHello).toBe(true);
});
});
// OriginalResource.spec
describe('service: original resource', function () {
var $httpBackend;
var $rootScope;
var Original;
beforeEach(inject(function (_Original_) {
Original = _Original_;
spyOn(Original, 'sayHelloSuccess').and.callThrough();
spyOn(Original, 'sayHelloFailure').and.callThrough();
}));
beforeEach(inject(function ($injector) {
$httpBackend = $injector.get('$httpBackend');
$rootScope = $injector.get('$rootScope');
OriginalResource = $injector.get('OriginalResource');
}));
describe('sending a request to say hello', function() {
describe('on success', function () {
it('calls sayHelloSuccess', function() {
$httpBackend.expect('GET', '/say-hello').respond(200, 'success');
OriginalResource.sayHello("your name");
$httpBackend.flush();
expect(Original.sayHelloSuccess).toHaveBeenCalled();
expect(Original.sayHelloFailure).not.toHaveBeenCalled();
expect(Original.saidHello).toBe(true, 'OriginalResource.sayHello has succeeded');
});
});
describe('on failure', function () {
it('calls sayHelloFailure', function() {
$httpBackend.expect('GET', '/say-hello').respond(404, 'fail');
OriginalResource.sayHello("your name");
$httpBackend.flush();
expect(Original.sayHelloSuccess).not.toHaveBeenCalled();
expect(Original.sayHelloFailure).toHaveBeenCalled();
expect(Original.saidHello).toBe(false, 'OriginalResource.sayHello has failed');
});
});
});
});
});
</script>
<script id="jsbin-source-javascript" type="text/javascript">//--- CODE --------------------------
(function (angular) {
var myApp = angular.module('myApp', ['ngResource']);
// Original Service
myApp.service('Original', function () {
var self = this;
this.saidHello = false;
this.sayHelloSuccess = function () {
self.saidHello = true;
console.log('success!');
};
this.sayHelloFailure = function (error) {
self.saidHello = false;
console.log('failure: ' + error);
};
});
// Original Resource Service
myApp.service('OriginalResource', function ($resource, Original) {
var resource = $resource('/say-hello');
this.sayHello = function (name) {
return resource.get({}, { name: name }).$promise.then(
function (response) {
Original.sayHelloSuccess();
},
function (error) {
Original.sayHelloFailure(error);
}
);
}
});
})(angular);
// ---SPECS-------------------------
describe('myApp', function () {
beforeEach(function () {
module('myApp');
});
// Original.spec
describe('service: original', function () {
var Original;
beforeEach(inject(function (_Original_) {
Original = _Original_;
}));
it('says hello', function() {
Original.sayHelloSuccess();
expect(Original.saidHello).toBe(true);
});
});
// OriginalResource.spec
describe('service: original resource', function () {
var $httpBackend;
var $rootScope;
var Original;
beforeEach(inject(function (_Original_) {
Original = _Original_;
spyOn(Original, 'sayHelloSuccess').and.callThrough();
spyOn(Original, 'sayHelloFailure').and.callThrough();
}));
beforeEach(inject(function ($injector) {
$httpBackend = $injector.get('$httpBackend');
$rootScope = $injector.get('$rootScope');
OriginalResource = $injector.get('OriginalResource');
}));
describe('sending a request to say hello', function() {
describe('on success', function () {
it('calls sayHelloSuccess', function() {
$httpBackend.expect('GET', '/say-hello').respond(200, 'success');
OriginalResource.sayHello("your name");
$httpBackend.flush();
expect(Original.sayHelloSuccess).toHaveBeenCalled();
expect(Original.sayHelloFailure).not.toHaveBeenCalled();
expect(Original.saidHello).toBe(true, 'OriginalResource.sayHello has succeeded');
});
});
describe('on failure', function () {
it('calls sayHelloFailure', function() {
$httpBackend.expect('GET', '/say-hello').respond(404, 'fail');
OriginalResource.sayHello("your name");
$httpBackend.flush();
expect(Original.sayHelloSuccess).not.toHaveBeenCalled();
expect(Original.sayHelloFailure).toHaveBeenCalled();
expect(Original.saidHello).toBe(false, 'OriginalResource.sayHello has failed');
});
});
});
});
});
</script></body>
</html>
//--- CODE --------------------------
(function (angular) {
var myApp = angular.module('myApp', ['ngResource']);
// Original Service
myApp.service('Original', function () {
var self = this;
this.saidHello = false;
this.sayHelloSuccess = function () {
self.saidHello = true;
console.log('success!');
};
this.sayHelloFailure = function (error) {
self.saidHello = false;
console.log('failure: ' + error);
};
});
// Original Resource Service
myApp.service('OriginalResource', function ($resource, Original) {
var resource = $resource('/say-hello');
this.sayHello = function (name) {
return resource.get({}, { name: name }).$promise.then(
function (response) {
Original.sayHelloSuccess();
},
function (error) {
Original.sayHelloFailure(error);
}
);
}
});
})(angular);
// ---SPECS-------------------------
describe('myApp', function () {
beforeEach(function () {
module('myApp');
});
// Original.spec
describe('service: original', function () {
var Original;
beforeEach(inject(function (_Original_) {
Original = _Original_;
}));
it('says hello', function() {
Original.sayHelloSuccess();
expect(Original.saidHello).toBe(true);
});
});
// OriginalResource.spec
describe('service: original resource', function () {
var $httpBackend;
var $rootScope;
var Original;
beforeEach(inject(function (_Original_) {
Original = _Original_;
spyOn(Original, 'sayHelloSuccess').and.callThrough();
spyOn(Original, 'sayHelloFailure').and.callThrough();
}));
beforeEach(inject(function ($injector) {
$httpBackend = $injector.get('$httpBackend');
$rootScope = $injector.get('$rootScope');
OriginalResource = $injector.get('OriginalResource');
}));
describe('sending a request to say hello', function() {
describe('on success', function () {
it('calls sayHelloSuccess', function() {
$httpBackend.expect('GET', '/say-hello').respond(200, 'success');
OriginalResource.sayHello("your name");
$httpBackend.flush();
expect(Original.sayHelloSuccess).toHaveBeenCalled();
expect(Original.sayHelloFailure).not.toHaveBeenCalled();
expect(Original.saidHello).toBe(true, 'OriginalResource.sayHello has succeeded');
});
});
describe('on failure', function () {
it('calls sayHelloFailure', function() {
$httpBackend.expect('GET', '/say-hello').respond(404, 'fail');
OriginalResource.sayHello("your name");
$httpBackend.flush();
expect(Original.sayHelloSuccess).not.toHaveBeenCalled();
expect(Original.sayHelloFailure).toHaveBeenCalled();
expect(Original.saidHello).toBe(false, 'OriginalResource.sayHello has failed');
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment