Reputation: 851
Hi I am trying to write some code for a controller called daterangecontroller which calls a service to give it some values for start and end date (these are part of a calendar widget).
The date range service provides defaults for start and end dates..and then the controller watches for changes in the start and end date model. When there are changes it calls the service to update a date range interval which is eventually called by another service.
I am haivng trouble understanding why this conditions fails if the error says:
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)
Here are the relevant parts of my code.
Controller:
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);
}
});
});
And the service
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;
}
};
}]);
UPDATE: Here's the jasmine code
"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);
});
});
Upvotes: 0
Views: 704
Reputation: 1360
Use toEqual(), not toBe(), toEqual checks equivalence, and toBe checks if they are the exact same object.
This can be really slow if you have a large suite of tests, and I would recommend splitting it up.
Upvotes: 1