3

$httpを使用するメソッドでspyonを使用して、JavaScriptでジャスミンテストを作成しようとしています。私は $httpBackend を使用してこれを嘲笑しましたが、残念ながらスパイは、メソッドが実際に $http の使用後に呼び出されたという事実を認識していないようです。デバッグで呼び出されていることがわかるので、呼び出されていないと報告する理由がわかりません。スコープの使用に問題があるのではないでしょうか? または $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);
});
4

1 に答える 1