Angular Integration Tests
Performing integration tests with angular-mocks.
by bullrout
HTML
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.2.1/jasmine.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.2.1/jasmine.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.2.1/jasmine-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.2.1/boot.min.js"></script>
<script src="https://code.angularjs.org/1.3.9/angular.min.js"></script>
<script src="https://code.angularjs.org/1.3.9/angular-mocks.js"></script>
JavaScript
(function (app) {
app.factory("oDataSvc", ['$q', '$http', function ($q, $http) {
return {
checkEndPoint: function () {
var deferred = $q.defer();
$http.get("http://services.odata.org/V4/TripPinServiceRW")
.then(function () {
deferred.resolve(true);
}, function () {
deferred.resolve(false);
});
return deferred.promise;
}
};
}]);
})(angular.module('test', []));
(function () {
var url = "http://services.odata.org/V4/TripPinServiceRW";
describe("jasmine", function () {
it("works", function () {
expect(true).toBe(true);
});
});
describe("angular unit test", function () {
var oDataService, httpBackend;
beforeEach(function () {
module("test");
});
beforeEach(inject(function ($httpBackend, oDataSvc) {
httpBackend = $httpBackend;
oDataService = oDataSvc;
}));
afterEach(function () {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
it("is registered with the module.", function () {
expect(oDataService).not.toBeNull();
});
describe("checkEndPoint", function () {
it("should return true upon successful connection", function () {
oDataService.checkEndPoint()
.then(function (result) {
expect(result).toEqual(true);
}, function () {
expect(false).toBe(true);
});
httpBackend.expectGET(url).respond(200, null);
httpBackend.flush();
});
it("should return false upon failed connection", function () {
oDataService.checkEndPoint()
...