2015-01-02 38 views
3

我想在使用$ http使用spyon的一些JavaScript上寫一個茉莉花測試。我使用$ httpBackend嘲笑了這一點,不幸的是,間諜似乎並沒有發現這個方法確實被稱爲post $ http useage。我可以看到它在調試中被調用,所以不確定爲什麼它報告它沒有被調用。我懷疑我的範圍使用有問題?或$順序httpBackend.flush \驗證:

代碼測試

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

測試不起作用(但應該。返回計數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); 
}); 

回答

2

的問題是你打電話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); 
})); 

您可能需要從測試中刪除後沖水聲明,但它可能不應該有反正因爲通常它的測試行爲的核心部分,應該之前預期的陳述。