单元测试控制器 - 需要第二副眼睛

时间:2015-01-09 20:01:36

标签: angularjs unit-testing

您好我需要帮助为此控制器功能编写测试。我想我很接近,但我一直将预期未定义为等于{entityId:2,page:19,length:200}。这看起来很奇怪,因为我有另一个几乎相同的测试。如果有人能发现错误或有任何一般的测试建议,我会非常感激。

谢谢!

   $scope.getAll = function (page, length) {

        accountService.getAccounts(global.activeOrganizationId, page, length).then(function (data) {
            $scope.accounts = data;
        });

    };

这是我到目前为止所拥有的

describe('AccountsController', function () {
    //make module avalible to tests
    beforeEach(module('pb.accounts.controllers'));
    beforeEach(module('ui.router'));
    beforeEach(module('ui.bootstrap'));

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

    var mockAccountSrv = {
        account: {
            entityId: 2,
            page: 19,
            length: 200
        }
    };

    // instantiating controller
    beforeEach(inject(function (_$controller_) {
        // The injector unwraps the underscores (_) from around the parameter names when matching
        $controller = _$controller_;
    }));

describe("getAll() function", function() {
        var controller, scope;


        beforeEach(inject(function ($q) {
            mockAccountSrv.getAccounts = function (entityId, page, length) {
                var defer = $q.defer();
                defer.resolve(this.account);
                return defer.promise;
            };
        }));

        // sets scope of controller before each test
        beforeEach(inject(function ($rootScope) {
            scope = $rootScope.$new();
            controller = $controller('AccountsController',
                {
                    $scope: scope,
                    $stateParams: mockStateParams,
                    global: mockGlobal,
                    accountService: mockAccountSrv
                });
        }));

        it("make sure service promise resolves", function () {
            scope.getAll(mockAccountSrv.account.page, mockAccountSrv.account.length);
            scope.$digest();

            expect(scope.account).toEqual(mockAccountSrv.account);
        });

    });

1 个答案:

答案 0 :(得分:0)

如果有人想知道我使用mockAccountSrv.account而不是.accounts做了一个愚蠢的拼写错误 谢谢你看!并感谢Nikos