如何编写以下角度服务的测试用例

时间:2017-04-24 07:27:58

标签: angularjs karma-jasmine

我一直在尝试为下面的服务函数编写Jasmine测试用例,但是它失败了,请看下面的代码,我把它写下来一个服务“getAllNotificationList”带两个参数一个嵌套数组和一个数字并合并嵌套数组数据和创建新数据,工作正常,尝试使用测试用例及其失败测试相同。

控制器代码:

angular.module('mobilityApp')
            .service('manageNotifications', function(CommonDatahub, API_ENDPOINT, $window) {

            /**
             * @ngdoc function
             * @name getAllNotificationList
             * @method Of mobilityApp.service:getAllNotificationList
             * @description
             * prepare all notification list
             */
            this.getAllNotificationList = function(_arrData, _fmno) {
                var masterList = _arrData[0],
                        isUserList = _arrData[1].length > 0 ? true : false,
                        userList = _arrData[1].length > 0  ? this.convertArrToObj(_arrData[1]) : false,
                        allNotificationList = [],
                        count = 0;

                masterList.forEach(function(objVal) {
                    var obj = {},
                            listName = objVal['name'];

                    if( isUserList &&  userList[listName] !== undefined ) {
                        obj = userList[listName];
                        obj['elemPos'] = count;
                    } else {
                        obj['mobilityEventType'] = objVal;
                        obj['userPreference'] = true;
                        obj['fmno'] = _fmno;
                        obj['elemPos'] = count;
                    }
                    count++;
                    allNotificationList.push(obj);
                });
                console.log(JSON.stringify(allNotificationList));
                return allNotificationList;
            };

        });

服务代码:

   "use strict";

describe("ManageNotification API service test", function () {
  var manageNotifications, CommonDatahub;

  beforeEach(module("mobilityApp"));

  beforeEach(inject(function (_manageNotifications_, _CommonDatahub_) {
    manageNotifications = _manageNotifications_;
  }));


  var _fmno = 84194,
      _arrData = [[{
                    "mobilityEventType": "GENERAL",
                    "description": "Non Partner transfer created",
                    "defaultSelected": true,
                    "name": "NP_TRANSFER_CREATED"
                  }], [{
                    "id": 100000,
                    "mobilityEventType": {
                      "mobilityEventType": "GENERAL",
                      "description": "Non Partner transfer created",
                      "defaultSelected": true,
                      "name": "NP_TRANSFER_CREATED"
                    },
                    "userPreference": false,
                    "fmno": 84194
                }]],
      _allListData = [{
                      "mobilityEventType": {
                        "mobilityEventType": "GENERAL",
                        "description": "Non Partner transfer created",
                        "defaultSelected": true,
                        "name": "NP_TRANSFER_CREATED"
                      },
                      "userPreference": false,
                      "fmno": 84194,
                      "elemPos": 0
                    },
                    {
                      "id": 100000,
                      "mobilityEventType": {
                        "mobilityEventType": "GENERAL",
                        "description": "Non Partner transfer created",
                        "defaultSelected": true,
                        "name": "NP_TRANSFER_CREATED"
                      },
                      "userPreference": false,
                      "fmno": 84194,
                      "elemPos": 1
                    }];


  it('should merge master and user list and create new list', function() {
    var result = manageNotifications.getAllNotificationList(_arrData, _fmno);
    result.toEqual(_allListData);
  });

});

1 个答案:

答案 0 :(得分:0)

在您的测试中,您有拼写错误:

result.toEqualt();

删除't'。