1

我需要一些帮助来格式化我的单元测试以从 testacular/karma 获得预期的结果

beforeEach( inject( function( customService, $httpBackend ){            
         service = customService;
         $http = $httpBackend
;}))

it('should make a default call upon initialization',function(){

var modalObj = {
                 "id": null,
                 "contentId": null,
                 "isActive":false,
                 "websiteId":2000,
                 "createdByUserName":"haselton",
                 "text":"another micro",
                 "tags":[],
                 "something random":null,
                 "something else":3
              },
              requestObj = {"isActive":false,"tags":[],"websiteId":2000,"createdByUserName":"haselton","text":"another micro"},
            mbObj = {};


         $http.whenPOST('/url/of/call', [requestObj]).respond(
             {
                 "id": 40,
                 "contentId": null,
                 "isActive": false,
                 "creationDate": "05/22/2013 14:24:14 PDT",
                 "lastModifiedDate": "05/22/2013 14:24:14 PDT",
                 "lastModifiedStatusDate": null,
                 "websiteId": 2000,
                 "createdByUserName": "haselton",
                 "text": "another micro",
                 "tags": []
             }
         );

然后我有一个服务会产生调用并返回一个承诺。以下是测试的其余部分:

         mbObj = service.createEntry(modalObj); // returns a promise
         mbObj.then(function(data){
             console.log(data);
             expect( angular.isNumber(data.id)).toBe(true);
             expect(data.isActive).toBe(false);
             expect(data.tags).toBe( 0 );
             expect(data.websiteId).toBe(2000);
             expect(data.createdByUserName).toBe("haselton");
             expect(data.text).toBe("another micro");
         }); 

         $http.flush();

});

实现的承诺中的 console.log 没有通过,而是注销了 Angular 资源。这是一个屏幕截图: 在此处输入图像描述

关于我在这里做错了什么的任何想法?谢谢!

4

1 回答 1

0

data为请求解析的参数modalObj是一个数组,因此使用下标获取实际数据:

console.log(data[0], data[1], data[2], data[3] , data[4], data[5], data[6], data[7], data[8], data[9]);
于 2013-12-10T19:51:08.167 回答