Skip to content

Instantly share code, notes, and snippets.

@sumitk
Last active February 1, 2016 07:10
Show Gist options
  • Save sumitk/5a29b929ebfa540c5d0a to your computer and use it in GitHub Desktop.
Save sumitk/5a29b929ebfa540c5d0a to your computer and use it in GitHub Desktop.

Revisions

  1. sumitk revised this gist Feb 1, 2016. No changes.
  2. sumitk created this gist Feb 1, 2016.
    43 changes: 43 additions & 0 deletions Angular Service test code
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    angular.module('myApp.services', ['ngResource'])
    .factory('TestingService', ['$resource', function($resource) {
    return $resource('/api/v1/values', {}, {
    getValues: {
    method: 'GET'
    }
    });
    }]);


    describe('Testing services', function() {
    beforeEach(function(){
    module('myApp.services');
    this.addMatchers({
    toEqualData: function(expected) {
    return angular.equals(this.actual, expected);
    }
    });
    });

    afterEach(function() {
    inject(function($httpBackend) {
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
    });
    });

    describe('TestingService', function() {
    it('should work',
    inject(['$rootScope', '$httpBackend', 'TestingService',
    function ($rootScope, $httpBackend, testingService) {

    $httpBackend.expectGET('/api/v1/values').respond( { key: 'value' } );

    var result = testingService.getValues();

    $httpBackend.flush();
    expect(result).toEqualData( { key: 'value' } );
    alert(angular.toJson(result, true));
    }])
    );
    });
    });