3

angual.module('app')和 和有什么不一样module('app')

这是有问题的简单服务和单元测试:

服务

(function () {
    "use strict"

    var app = angular.module('app', []);

    app.service('CustomerService', ['$http', function ($http) {
        return {
            getById: function (customerId) {
                return $http.get('/Customer/' + customerId);
            }
        }
    }]);
}());

测试

describe('Customer Service', function () {
    var $rootScope,
        $httpBackend,
        service,
        customerId = 1;

    beforeEach(function () {
        angular.module('app', ['ngMock']);

        inject(function ($injector) {
            $rootScope = $injector.get('$rootScope');

            $httpBackend = $injector.get('$httpBackend');
            $httpBackend.whenGET('/Customer/' + customerId).respond({ id: customerId, firstName: 'Joe', lastName: 'Blow' });

            service = $injector.get('CustomerService');
        });
    });

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

    it('should get customer by id', function () {
        var customer;

        service.getById(1).then(function (response) {
            customer = response.data;
        });

        $httpBackend.flush();
        expect(customer.firstName).toBe('Sam');
    });
});
4

1 回答 1

6

module在单元测试框架中指的是mockangular.mock.module方法(为了方便,附在window上)。angular.moduleangular.mock.module模拟的方法。

于 2013-11-01T19:43:37.653 回答