【问题标题】:Jasmine test using spyon with $httpBackend not working使用带有 $httpBackend 的 spyon 的 Jasmine 测试不起作用
【发布时间】:2015-02-28 17:13:50
【问题描述】:

我正在尝试使用 spyon 在使用 $http 的方法上对一些 javascript 编写 jasmine 测试。我已经使用 $httpBackend 模拟了这一点,不幸的是,间谍似乎没有意识到该方法确实被称为 post $http useage。我可以看到它在调试中被调用,所以不确定为什么它报告它没有被调用。我怀疑我的示波器使用有问题?或 $httpBackend.flush\verify 的顺序?:

正在测试的代码

function FileUploadController($scope, $http, SharedData, uploadViewModel) {

   Removed variables for brevity
   .....

    $scope.pageLoad = function () {
        $scope.getPeriods();

        if ($scope.uploadViewModel != null && $scope.uploadViewModel.UploadId > 0) {
            $scope.rulesApplied = true;
            $scope.UploadId = $scope.uploadViewModel.UploadId;

            $scope.linkUploadedData();
        } else {
            $scope.initDataLinkages();
        }

    }


    $scope.initDataLinkages = function () {

        $http({ method: "GET", url: "/api/uploadhistory" }).
           success(function (data, status) {
               $scope.status = status;
               $scope.setUploadHistory(data);

           }).
         error(function (data, status) {
             $scope.data = data || "Request failed";
             $scope.status = status;
         });

    }

    $scope.setUploadHistory = function (data) {

        if ($scope.UploadId > 0) {
            $scope.currentUpload = data.filter(function (item) {
                return item.UploadId === $scope.UploadId;
            })[0];

            //Remove the current upload, to prevent scaling the same data!
            var filteredData = data.filter(function (item) {
                return item.UploadId !== $scope.UploadId;
            });
            var defaultOption = {
                UploadId: -1,
                Filename: 'this file',
                TableName: null,
                DateUploaded: null
            };

            $scope.UploadHistory = filteredData;

            $scope.UploadHistory.splice(0, 0, defaultOption);
            $scope.UploadHistoryId = -1;

            $scope.UploadTotal = $scope.currentUpload.TotalAmount;

        } else {
            $scope.UploadHistory = data;
        }
    }

测试设置

beforeEach(module('TDAnalytics'));
beforeEach(inject(function (_$rootScope_, $controller, _$httpBackend_) {
    $rootScope = _$rootScope_;
    $scope = $rootScope.$new();
    $httpBackend = _$httpBackend_;

    var sharedData = { currentBucket: { ID: 1 } };

    controller = $controller('FileUploadController', { $scope: $scope, SharedData: sharedData, uploadViewModel: null }); 

    $httpBackend.when('GET', '/api/Periods').respond(periods);

    $httpBackend.when('GET', '/api/uploadhistory').respond(uploadHistory);


    $scope.mappingData = {
        FieldMappings: [testDescriptionRawDataField, testSupplierRawDataField],
        UserFields: [testDescriptionUserField, testSupplierUserField]
    };
}));

afterEach(function() {
    testDescriptionRawDataField.UserFields = [];
    testSupplierRawDataField.UserFields = [];
    testTotalRawDataField.UserFields = [];

    $httpBackend.flush();
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
});

工作测试:

it('pageLoad should call linkUploadedData when user has navigated to the page via the Data Upload History and uploadViewModel.UploadId is set', function () {
    // Arrange
    spyOn($scope, 'linkUploadedData');
    $scope.uploadViewModel = {UploadId: 1};
    // Act
    $scope.pageLoad();

    // Assert
    expect($scope.rulesApplied).toEqual(true);
    expect($scope.linkUploadedData.calls.count()).toEqual(1);
});

不起作用的测试(但应该。返回 count-0 但被调用)

it('pageLoad should call setUploadHistory when data returned successfully', function () {
    // Arrange
    spyOn($scope, 'setUploadHistory');
    // Act
    $scope.initDataLinkages();

    // Assert
    expect($scope.setUploadHistory.calls.count()).toEqual(1);
});

【问题讨论】:

    标签: javascript angularjs jasmine httpbackend spyon


    【解决方案1】:

    问题是您在期望之后调用 httpBackend.flush(),这意味着在您进行测试之后调用成功。您必须在期望语句之前刷新。

    it('pageLoad should call setUploadHistory when data returned successfully',
    inject(function ($httpBackend, $rootScope) {
        // Arrange
        spyOn($scope, 'setUploadHistory');
        // Act
        $scope.initDataLinkages();
        $httpBackend.flush();
        $rootScope.$digest()
        // Assert
           expect($scope.setUploadHistory.calls.count()).toEqual(1);
    }));
    

    您可能需要在测试之后删除 flush 语句,但它可能无论如何都不应该存在,因为它通常是测试行为的核心部分,应该在期望语句之前。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 2020-04-08
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多