I'm setting a date value using moments.js in my controller. I'm getting following error when running trying to run a spec against it:
TypeError: 'undefined' is not a function (evaluating 'moment()')
I don't care about testing that the value is set correctly, I just want the date value to be defined to allow the test to proceed. What's the best way to mock this? I've tried using jasmine.clock() as suggested in related questions, but the test is still trying to evaluate moment(). Ideally I would be able to just set controller.date to some arbitrary value for test purposes before each test.
Here's the code in my controller (set date is called on controller initialization):
function setDate() {
controller.date = {
startDate: moment().subtract(1, "days").hour(0).minute(0).second(0),
endDate: moment()
};
}
EDIT:
Here's the relevant controller code:
.controller('OrderListCtrl', function ($rootScope, $state, $stateParams, $uibModal, $q, orders, Orders, Product, Store, Payments, params) {
var controller = this;
// set default values for datepicker - beginning of yesterday until current time today
controller.setDate = function() {
controller.date = {
startDate: moment().subtract(1, "days").hour(0).minute(0).second(0),
endDate: moment()
};
}
controller.setDate();
});
Here's the relevant spec code:
describe("OrderController", function() {
var orderController, deferred, params, scope, state, stateParams, uibModal, q, controller, moment;
beforeEach(inject(function ($controller, $rootScope, $q, $uibModal) {
scope = $rootScope.$new();
q = $q;
controller = $controller;
uibModal = $uibModal;
deferred = $q.defer();
orderController = controller('OrderListCtrl', {
$rootScope: scope,
$state: state,
$stateParams: stateParams,
$uibModal: uibModal,
$q: q
});
orderController.setDate = function() {
orderController.date = {
startDate: new Date('01/01/2016'),
endDate: new Date('01/02/2016')
}
}
}));
Trying to override the setDate() method in this way apparently isn't working, it's still throwing an error: "can't find moment variable".
Dateobjects andmomentobjects are two separate things. Why are you trying to useDateobjects in one instance andmomentobjects in another? - Matt Johnson-Pint