使用$ httpbackend进行单元测试服务

时间:2015-01-13 00:59:05

标签: angularjs unit-testing

这是我第一次尝试以角度测试服务,这让我对启动非常困惑。我正在测试的代码是:

angular.module('pb.accounts.services')
    .service('accountService', ['$q', 'apiResource', function ($q, apiResource) {

        this.getAccounts = function (orgId, page, length) {
            var deferred = $q.defer();

            apiResource.get('/:orgId/Accounts/:page/:length', { orgId: orgId, page: page, length: length })
            .then(function (data, status, headers) {
                deferred.resolve(data);
            }, function (data, status, headers) {
                deferred.reject("There was a problem getting accounts.");
            });

            return deferred.promise;
        };

        this.getAccount = function (orgId, entityId) {
            var deferred = $q.defer();

            apiResource.get('/:orgId/Accounts/:entityId', { orgId: orgId, entityId: entityId })
            .then(function (data, status, headers) {
                deferred.resolve(data);
            }, function (data, status, headers) {
                deferred.reject("There was a problem getting the account.");
            });

            return deferred.promise;
        };

        this.createAccount = function (orgId, account) {
            var deferred = $q.defer();

            apiResource.post('/:orgId/Accounts', { orgId: orgId }, account)
            .then(function (data, status, headers) {
                deferred.resolve(data);
            }, function (data, status, headers) {
                deferred.reject(data);
            });

            return deferred.promise;
        };

        this.updateAccount = function (orgId, account) {
            var deferred = $q.defer();

            apiResource.put('/:orgId/Accounts', { orgId: orgId }, account)
            .then(function (data, status, headers) {
                deferred.resolve(data);
            }, function (data, status, headers) {
                deferred.reject(data);
            });

            return deferred.promise;
        };

        this.deleteAccount = function (orgId, entityId) {
            var deferred = $q.defer();

            apiResource.delete('/:orgId/Accounts/:entityId', { orgId: orgId, entityId: entityId })
            .then(function (data, status, headers) {
                deferred.resolve(data);
            }, function (data, status, headers) {
                deferred.reject("There was a problem deleting this account.");
            });

            return deferred.promise;
        };

    }]);

我的初始设置尝试是这样的

// testing controller
describe('accountService', function() {
    var accountService, httpBackend;

    var mockApiResourceProvider = {};
    var mockGlobal = { activeOrganizationId: 0 };
    var mockStateParams = { orgId: 1, entityId: null };
    var mockForm = {};
    var mockState = {};

    // Set up the module
    beforeEach(module('pb.accounts.services'));
    //beforeEach(module('apiResourceProvider'));

    beforeEach(function () {
        inject(function ($httpBackend, _accountService_) {
            accountService = _accountService_;
            httpBackend = $httpBackend;
            apiResourceProvider = mockApiResourceProvider;
        });
    });


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

    it('should send the msg and return the response.', function () {
        var returnData = { orgId: 5, status: 200, headers: "I did it (maybe)" };

        httpBackend.expectGET('/:orgId/Accounts/:page/:length').respond(returnData);

        var returnedPromise = accountService.sendMessage('wee');

        // set up a handler for the response, that will put the result
        // into a variable in this scope for you to test.
        var result;
        returnedPromise.then(function (response) {
            result = response;
        });

        httpBackend.flush();
        expect(result).toEqual(returnData);
    });
});

这看起来好像开始吗?

0 个答案:

没有答案