2014-06-19 180 views
0

嗨我想寫一個控制器的一些代碼調用daterangecontroller調用服務給它一些值的開始和結束日期(這些是日曆小部件的一部分)。茉莉花測試...測試失敗,但條件是真

日期範圍服務提供了開始和結束日期的默認值。然後控制器監視開始日期和結束日期模型的更改。當有變化時,它會調用服務來更新最終由另一個服務調用的日期範圍間隔。

我haivng很難理解爲什麼如果錯誤是這個條件失敗:

Chrome 35.0.1916 (Mac OS X 10.9.2) Controller: dateRangeCtrl default start date loads as  8 days ago FAILED 
Expected Date(Wed Jun 11 2014 11:04:11 GMT-0700 (PDT)) to be Date(Wed Jun 11 2014 11:04:11 GMT-0700 (PDT)). 
Error: Expected Date(Wed Jun 11 2014 11:04:11 GMT-0700 (PDT)) to be Date(Wed Jun 11 2014 11:04:11 GMT-0700 (PDT)). 
    at null.<anonymous> (/Users/test/client/spec/controllers/dateRangeTest.js:32:28) 

這裏是我的代碼的相關部分。

控制器:

angular.module("vizApp").controller("DaterangeCtrl", function ($rootScope, $scope, DateRangeService) { 
"use strict"; 

// Dates to fill in the calendar widget 
var dates_current_interval = DateRangeService.giveCurrentInterval(); 
var dates_array = dates_current_interval.split("/"); 
$scope.startDate = new Date(dates_array[0]); 
$scope.endDate = new Date (dates_array[1]); 
var dateRange = {}; 

// this model watches for changes in the "from" calendar section 
$scope.$watch("startDate", function (newVal, oldVal, scope) { 

    if (newVal !== oldVal && newVal !== null && $scope.startDate) { 

     dateRange.start = new Date($scope.startDate); 
     dateRange.end = new Date($scope.endDate); 


     return DateRangeService.updateDateInterval(dateRange); 

    } 
}); 

// this model watches for changes in the "to" calendar section 
$scope.$watch("endDate", function (newVal, oldVal, scope) { 

    if (newVal !== oldVal && newVal !== null && $scope.startDate) { 

     dateRange.start = new Date($scope.startDate); 
     dateRange.end = new Date($scope.endDate); 

     return DateRangeService.updateDateInterval(dateRange); 


    } 
}); 

}); 

,服務

angular.module("vizApp").factory("DateRangeService", [ "$rootScope", function() { 
"use strict"; 
return{ 

    dateInterval: null, 

    /** 
    * 
    * @param : none 
    * returns either a default to fill in the input boxes or the updated dateInterval 
    * 
    */ 
    giveCurrentInterval: function giveCurrentInterval(){ 
     if (this.dateInterval === null){ 
      var startDate = new Date(); 
      startDate.setDate(startDate.getDate() - 8); 
      var endDate = new Date(); 
      var dateFill = startDate.toISOString() + "/" + endDate.toISOString(); 
      return dateFill; 
     } 
     else{ 
      return this.dateInterval; 
     } 
    }, 

    /** 
    * 
    * @param dateRange : the date range passed in from the model being watched 
    * returns the new dateInterval 
    * 
    * 
    */ 
    updateDateInterval: function updateDateInterval(dateRange){ 

     this.dateInterval = dateRange.start.toISOString() + "/" + dateRange.end.toISOString(); 
     return this.dateInterval; 
    } 



}; 
}]); 

更新:這是茉莉花代碼

"use strict"; 

describe("Controller: dateRangeCtrl", function() { 

    var scope, DateRangeController, httpBackend; 


    //load the controller's module 
    beforeEach(module('vizApp')); 
    beforeEach(function() { 
         angular.mock.inject(function ($injector) { 
          httpBackend = $injector.get("$httpBackend"); 

         }); 
        }); 

    //initialize the controller and a mock scope 
    beforeEach(inject(function($controller, $rootScope) { 
     // create a new child scope 
     scope = $rootScope.$new(); 
     //create a new instance of date range controller 
     DateRangeController = $controller('DaterangeCtrl', { $scope : scope }); 

    })); 

    it('default start date loads as 8 days ago', function(){ 
     var mockStartDate = new Date(); 
     mockStartDate.setDate(mockStartDate.getDate() - 8); 

     httpBackend.expectGET('partials/landing.html'); 
     httpBackend.whenGET('partials/landing.html').respond(200); 
     expect(scope.startDate).toBe(mockStartDate); 

    }); 



    it('changes date range when start date is changed', function(){ 
     var mockStartDate2 = new Date(); 
     mockStartDate2.setDate(mockStartDate2.getDate() - 10); 

     httpBackend.expectGET('partials/landing.html'); 
     httpBackend.whenGET('partials/landing.html').respond(200); 

     scope.$apply(function() { 
      scope.startDate = mockStartDate2; 
     }); 

     expect(scope.startDate).toBe(mockStartDate2); 

    }); 
}); 
+0

你可以發佈茉莉花代碼? –

+0

是的遺憾忘了那..在更新^^^^ – es3735746

回答

1

使用toEqual(),不砥(),toEqual檢查等價,並且要檢查它們是否是完全相同的對象。

如果您有大量測試,這可能會非常慢,我建議將其分開。

+0

如果這是一個$範圍的返回。$ watch語句....在我的代碼上面,我如何測試$ scope。$ watch回報我所期望的呢? – es3735746

+0

比較兩個空物體怎麼樣? 預期{}爲{}。 – es3735746