2014-06-19 106 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