【问题标题】:Jasmine unit test controllerJasmine 单元测试控制器
【发布时间】:2017-06-26 19:09:02
【问题描述】:

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

  1. 响应长度。将模拟数据指定为 [{ name: 'John', id: 1 }, { name: 'Josh', id: 2 }] 因此响应长度应为 2。此测试未通过,长度始终为 1。
  2. 响应数据应该相等。 IE。 expect(IndexSummaryService.getIndexSummaryQueues).toEqual([{ name: 'John', id: 1 }, { name: 'Josh', id: 2 }]);

测试失败并显示消息:Expected Function to equal [object({ name:'jo​​hn', id:1}), object({ name:'jo​​sh', 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;
        });
    }

我在jasmine中写过单元测试,下面是jasmine代码。

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 }]);
    }));
});

【问题讨论】:

    标签: angularjs unit-testing jasmine


    【解决方案1】:

    您没有测试承诺的响应,请尝试以下操作(可能不准确,因为我使用 Angular 已经有一段时间了,但基本上在承诺解决后,在 then 块中进行断言)。

    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();
        }));
    });
    

    【讨论】:

    • 谢谢李..它工作..添加了一个小改动..res.data.length 和 res.data..