使用逐步程序的angularjs的Jasmine测试用例

时间:2015-10-08 18:16:17

标签: javascript angularjs jasmine karma-jasmine

这是代码。我需要以下JS的茉莉花测试用例:我是茉莉花的新手,但我尝试了样品但不会工作。请帮帮我

app.js文件

var employeeApp = angular.module("myApp",[]);

employeeApp.controller("employeeController", function($rootScope, $scope, $http) {

            $http.get('data/employees.json').success(function(data) {

                $rootScope.employees = data;
            }); 

            var empId = 12342;
            $rootScope.saveEmployeeRecord = function(emp) {
                if($rootScope.employees.EmployeeId == null) {
                    $rootScope.employees.EmployeeId = empId++;
                    $rootScope.employees.push(emp);
                }
                else {
                    for(i in $rootScope.employees) {
                        if($rootScope.employees[i].EmployeeId == emp.EmployeeId) { 
                            $rootScope.employees[i]= emp;
                        }
                    }
                }

                //$rootScope.employees = {};
            }

            $rootScope.resetEmployeeRecord = function() {
                angular.copy({}, $rootScope.employees);
            }

            $rootScope.editEmployee = function(emp, indx) {

                    //$rootScope.emp = $rootScope.employees;
                    if($rootScope.employees[indx].EmployeeId == emp.EmployeeId) { 
                        $rootScope.employees = angular.copy($rootScope.employees[indx]);
                    }


            }

            $rootScope.DeleteEmployee = function(idx) {

                var result = confirm("Are you sure want to delete?");
                if (result) {
                    $rootScope.employees.splice(idx,1);
                    return true;
                }
                else {
                     return false;
                }

                //for(i in $rootScope.employees) {
                    //if($rootScope.employees[i].EmployeeId == idx) { 

                        //$rootScope.employees = {};
                    //}
                //}
            }

});

这是json文件

[
            {
                "EmployeeId": "61234",
                "LastName": "Anderson",
                "FirstName": "James",
                "Gender": "Male",
                "Email": "james_anderson@infosys.com",
                "Account": "Boeing"
            },
            {
                "EmployeeId": "512458",
                "LastName": "Cambell",
                "FirstName": "Mike",
                "Gender": "Male",
                "Email": "mike.cambell@infosys.com",
                "Account": "Boeing"
            },
            {
                "EmployeeId": "712785",
                "LastName": "Swachengar",
                "FirstName": "Andrew",
                "Gender": "Male",
                "Email": "andrew.swachengar@infosys.com",
                "Account": "Cisco"
            },
            {
                "EmployeeId": "712734",
                "LastName": "Anderson",
                "FirstName": "James",
                "Gender": "Male",
                "Email": "james.anderson@infosys.com",
                "Account": "Apple"
            },
            {
                "EmployeeId": "61245",
                "LastName": "Green",
                "FirstName": "Rachael",
                "Gender": "Female",
                "Email": "rachael_green@infosys.com",
                "Account": "Boeing"
            },
            {
                "EmployeeId": "61347",
                "LastName": "Brown",
                "FirstName": "Jackualine",
                "Gender": "Female",
                "Email": "jackualine_brown@infosys.com",
                "Account": "Boeing"
            }
] 

包含在规范runner.html

<script src="lib/jasmine-2.3.4/jasmine.js"></script>
  <script src="lib/jasmine-2.3.4/jasmine-html.js"></script>
  <script src="lib/jasmine-2.3.4/boot.js"></script>

  <!-- include source files here... -->
   <script type="text/javascript" src="../lib/Angular/angular.min.js"></script>
   <script type="text/javascript" src="../lib/Angular/angular-mocks.js"></script>

  <script type="text/javascript" src="../js/app.js"></script>

  <!-- include spec files here... -->
  <script type="text/javascript" src="spec/app_test.js"></script>

测试用例 app_test.js

'use strict'; describe('myApp',function(){

beforeEach(模块( '对myApp'));

describe(“employeeController”,function(){

   var scope, httpBackend;

    var mockEmployees = [{
        "EmployeeId": "61234",
            "LastName": "Anderson",
            "FirstName": "James",
            "Gender": "Male",
            "Email": "james_anderson@infosys.com",
            "Account": "Boeing"
    }, {
        "EmployeeId": "512458",
            "LastName": "Cambell",
            "FirstName": "Mike",
            "Gender": "Male",
            "Email": "mike.cambell@infosys.com",
            "Account": "Boeing"
    }, {
        "EmployeeId": "712785",
            "LastName": "Swachengar",
            "FirstName": "Andrew",
            "Gender": "Male",
            "Email": "andrew.swachengar@infosys.com",
            "Account": "Cisco"
    }, {
        "EmployeeId": "712734",
            "LastName": "Anderson",
            "FirstName": "James",
            "Gender": "Male",
            "Email": "james.anderson@infosys.com",
            "Account": "Apple"
    }];

    var mockedEditEmployeeService, emptyPromise;
   beforeEach(angular.mock.inject(function($controller, $rootScope, $httpBackend) {
       httpBackend = $httpBackend;



       scope = $rootScope;
       mockedEditEmployeeService = {
               editEmployee: emptyPromise
           };

       $controller('employeeController', {$scope: scope});
   }));

   it('should fetch list of users', function(){

       httpBackend.whenGET("../data/employees.json").respond(mockEmployees);

       httpBackend.expectGET("../data/employees.json");
       console.log(scope.employees);
       //httpBackend.flush();
       var spyEvent = spyOn(mockedEditEmployeeService, 'editEmployee').and.callThrough();;
       expect(mockedEditEmployeeService.editEmployee).toHaveBeenCalled();
    });

});

});

我已经尝试但是它显示了未定义的编辑员工的间谍和显示的其他一些错误。我尝试了更多的posiblity。但是没能做到。我是新手,请帮助我。

请提前帮助我。

0 个答案:

没有答案