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単体テスト フレームワークでは、モックangular.mock.moduleメソッドを参照します (便利なように window にアタッチされています)。モックangular.moduleするメソッドです。angular.mock.module

于 2013-11-01T19:43:37.653 に答える