Jasmine单元测试控制器

时间:2017-02-08 23:17:06

标签: angularjs unit-testing jasmine

我需要在这里检查两件事:

  1. 响应时长。将模拟数据指定为[{name:'John',id:1},{name:'Josh',id:2}],因此响应长度应为2.如果测试失败,则始终将长度设置为1。
  2. 响应数据应该相等。即。 expect(IndexSummaryService.getIndexSummaryQueues).toEqual([{name:'John',id:1},{name:'Josh',id:2}]);
  3. 测试失败,消息:预期函数等于[object({name:'john',id:1}),object({name:'josh',id:2})]

    我的服务有点不同,它以api作为参数,即URL。

    请建议如何使这些单元测试工作。

    这是服务代码

        app.service("IndexSummaryService", ['$http', function ($http) {
        this.getIndexSummaryQueues = function (api) {        
            return $http.get(api, { cache: false });
        };
         }]);
    

    这是控制器

     $scope.loadRecords = function (api) {
            $scope.loading = true;
            var GetIndexSummaryQueue = IndexSummaryService.getIndexSummaryQueues(api);
            GetIndexSummaryQueue.then(function (response) {
                $scope.Queues = response.data;
            }, function (error) {
                if (error.status == 500) {
                    $scope.errorStatus = "Error " + error.status;
                    $scope.errorMsg = error.data.message;
                }
                else {
                    $scope.errorStatus = "Error " + error.status;
                    $scope.errorMsg = GlobalConstants.errormessage;
                }
                $scope.errorpage = true;
                $scope.success = false;
                console.log("Status Data : " + error.data.message);
                console.log("Status Error : " + error.status);
            }).then(function () {
                $scope.loading = false;
            });
        }
    

    我在茉莉花下面写过单元测试是茉莉花的代码。

    describe("ISummary ->", function () {
    
    beforeEach(function () {
        module("ApplicationModule");
    });
    
    var $httpBackend;
    var scope, createController;
    
    beforeEach(inject(function ($rootScope, _$httpBackend_, $controller) {
        $httpBackend = _$httpBackend_;
        scope = $rootScope.$new();
        createController = function () {
            return $controller('IndexingSummaryController', {
                $scope: scope
            });
        };       
    
        $httpBackend.when("GET", "https://domain.com/captivaapi/api/capturestats/pldindexingSummary")
          .respond([{ name: 'John', id: 1 }, { name: 'Josh', id: 2 }]);
    }));
    
    afterEach(function () {
        $httpBackend.verifyNoOutstandingExpectation();
        $httpBackend.verifyNoOutstandingRequest();
    });
    
    describe("Service->", function () {
        it("can load topics", inject(function (IndexSummaryService) {          
            $httpBackend.expectGET("https://domain.com/captivaapi/api/capturestats/pldindexingSummary");
            IndexSummaryService.getIndexSummaryQueues('https://domain/captivaapi/api/capturestats/pldindexingSummary');
            $httpBackend.flush();
            expect(IndexSummaryService.getIndexSummaryQueues.length).toBeGreaterThan(0);
    
            expect(IndexSummaryService.getIndexSummaryQueues.length).toEqual(2);
    
            expect(IndexSummaryService.getIndexSummaryQueues).toEqual([{ name: 'John', id: 1 }, { name: 'Josh', id: 2 }]);
        }));
    });
    

1 个答案:

答案 0 :(得分:1)

你没有测试承诺的响应,试试下面的内容(可能不是很精确,因为我使用Angular已经有一段时间了,但是一旦承诺解决了,基本上会在一个块中进行断言)。 / p>

describe("Service->", function () {
    it("can load topics", inject(function (IndexSummaryService) {          
        $httpBackend.expectGET("https://domain.com/captivaapi/api/capturestats/pldindexingSummary");

        IndexSummaryService.getIndexSummaryQueues('https://domain/captivaapi/api/capturestats/pldindexingSummary').then(function(res) {
            expect(res.length).toBeGreaterThan(0);
            expect(res.length).toEqual(2);
            expect(res).toEqual([{ name: 'John', id: 1 }, { name: 'Josh', id: 2 }]);
        });
        $httpBackend.flush();
    }));
});