Within my Angular controller, I have the following function that retrieves data from a promise and I need to test if it returns the expected result.
function getName() {
var name = "";
nameService.getName().then(function (data) {
name = data.name;
});
return name;
}
I am unsure of how to mock the promise call with fake data. Can I use $httpBackend or $provide? I attempted the following but it did not work:
it("function getName should get the name from the nameService.getNameInfo function", function () {
var name = { name: "name1"};
spyOn(mockNameService, 'getNameInfo').and.callFake(function() {
return {
then: function(callback) {return callback(name);}
};
});
var result = myCtrl.getName();
expect(result).toEqual("name1");
});