I'm struggling with writing tests for a controller method that relies on an Angular service call and needs to test the code inside the .then() function afterwards.
Here's the basic concept:
// Controller
$scope.myMethod = function() {
methodName.connect().then(
function(returnValue) {
if (returnValue.prop === 1) {
$scope.callMade = true;
$rootScope.$emit('callWasMade');
}
},
function() {
$rootScope.$emit('callWasNotMade');
}
);
};
I want to test calling the method on the $scope, invoke the methodName.connect() function, mock a response by overriding returnValue, and ensure the function runs normally. Something like this:
// Test
describe('connecting to method', function() {
it('should do nothing when returnValue.prop does not equal 1', function() {
spyOn(methodName, 'connect').and.returnValue({ prop: 2 });
scope.myMethod();
expect(scope.callMade).toBeFalsy();
});
it('should pass when returnValue.prop does equal 1', function() {
spyOn(methodName, 'connect').and.returnValue({ prop: 1 });
scope.myMethod();
expect(scope.callMade).toBeTruthy();
});
});
However, I'm encountering the error:
undefined is not a constructor (evaluating 'methodName.connect().then') (...)
Is there a way to control the output from methodName.connect in this manner?
Thank you.