angular 测试

describe('Controllers: CheckListOverCtrl', function () {
    var $scope, ctrl, $httpBackend;
    var data = [];
    data.d = [{ name: 'name1', ProjectName: "ProjectName1", I_Category: "I_Category1", uName: "uName1", dName: "dName1", D_Create: "D_Create1" },
                    //{ name: 'name2', ProjectName: "ProjectName2", I_Category: "I_Category2", uName: "uName2", dName: "dName2", D_Create: "D_Create2" },
                    { name: 'name3', ProjectName: "ProjectName3", I_Category: "I_Category3", uName: "uName3", dName: "dName3", D_Create: "D_Create3" }];

    beforeEach(angular.mock.module('app.audit'));

    beforeEach(angular.mock.inject(function ($rootScope, $controller, _$httpBackend_) {
        $httpBackend = _$httpBackend_;
        $scope = $rootScope.$new();
        ctrl = $controller('CheckListOverCtrl', { $scope: $scope });
    }));

    it("标题测试", function () {
        expect($scope.$root.title).toBe('已审列表');
    });

    it("取数据测试", function () {
        expect($scope.list.length).toBe(0);
        $httpBackend.expectPOST('/Services/AuditWebForm.aspx/GetCheckListOver').respond(data);
        $httpBackend.expectPOST('/Services/UserAuth.aspx/GetUidList').respond('');
        $httpBackend.flush();
        expect($scope.list.length).toBe(2);
    });
    it("链接测试", function () {
        expect($scope.gethref(1, 1, 1)).toBe('../projectView/flowChart?id=1&type=1');
        expect($scope.gethref(2, 1, 1)).toBe('../budgetView/flowChart?id=1&ProjectId=1&type=2');
        expect($scope.gethref(3, 1, 1)).toBe('../contractView/flowChart?id=1&ProjectId=1&type=3');
        expect($scope.gethref(4, 1, 1)).toBe('../ExpenseView/flowChart?id=1&ProjectId=1&type=4');
        expect($scope.gethref(5, 1, 1)).toBe('../ReceiptView/flowChart?id=1&ProjectId=1&type=5');
        expect($scope.gethref(6, 1, 1)).toBe('../WorkView/flowChart?id=1&ProjectId=1&type=6');
    });
    it("type字符串测试", function () {
        expect($scope.typestring(1)).toBe('项目');
        expect($scope.typestring(2)).toBe('预算');
        expect($scope.typestring(3)).toBe('合同');
        expect($scope.typestring(4)).toBe('报销');
        expect($scope.typestring(5)).toBe('发票');
        expect($scope.typestring(6)).toBe('工单');
    });
    it("时间字符串测试", function () {
        expect($scope.datestring('2014-10-09T 09:43:00')).toBe('2014-10-09');
    });
});

  controller为audit。测试getlist方法。预先留好返回值,在测试的具体方法it()内,预设expectpost ,表明flush时会按预设的expensepost走。

某些时候,需要手动载入某方法。比如将页面加载数据的方法注释掉后,在it模块中执行要测试的方法比如A, 那在it模块一进来 就$scope.A(); 然后在写expectpost和flush。最后在对结果进行判断。测试方法是否正确。

 it('click测试', function () {
        $scope.click();
        $httpBackend.expectPOST('/Services/WorkListWebForm.aspx/WorkInsert').respond(data);
        $httpBackend.flush();
        expect($scope.spec).toBe('前台逻辑测试结束');
    });

  这里的click方法为页面上的提交按钮方法。 在页面的js文件中 加载页面数据的方法比如为load()方法。则需要在页面的js文件中将load()先注释掉,先不执行。不注释的话,这里$httpBackend.flush()会执行load中的逻辑 不会到click中,注释掉后,在it模块中如上编码。

以下为页面js代码片段。

    // 确定点击事件
        $scope.click = function () {

            $scope.work.Id = $scope.workid;
            $scope.work.I_Project = $scope.projectid;

            return $http.post('/Services/WorkListWebForm.aspx/WorkInsert', { entity: $scope.work }).success(function (data) {
                alert("提交成功");
                $scope.spec = "前台逻辑测试结束";//测试
                //window.location.href = "WorkList?ProjectId=" + $scope.projectid;
                $location.path("detail/WorkList").search({ "projectId": $scope.projectid });
            }).error(function () {
                $scope.list = "";
            });
        }

        $scope.reset = function () {
            if ($scope.workid > 0) {
                $scope.getdata();
            } else {
                $scope.C_Title = "";
                $scope.C_Phone = "";
                $scope.C_Email = "";
                $scope.C_Other = "";
                $scope.D_Hope = "";
                $scope.I_Type = 9;
                $scope.C_Need = "";
            }
        }

        //以下需注释原因是测试click方法。不加载getdata方法  

        //if ($scope.workid > 0) {
        //    $scope.$root.title = "工单编辑";
        //    $scope.getdata();
        //} else {
        //    $scope.$root.title = "新建工单";
        //    $scope.work.I_Type = 9;
        //}

 

posted on 2014-10-09 14:57  战马  阅读(318)  评论(0)    收藏  举报

导航