Let's suppose I have a controller like:
angular
.module("app", [])
.controller("NewsFeedController", [
"$scope",
"NewsFeedService",
function ($scope, NewsFeedService) {
$scope.news = [
{ stamp: 1 },
{ stamp: 9 },
{ stamp: 0 }
];
$scope.onScroll = function () {
/*
might do some stuff like debouncing,
checking if there's news left to load,
check for user's role, whatever.
*/
var oldestStamp = getOldestNews().stamp;
NewsFeedService.getOlderThan(oldestStamp);
/* handle the request, append the result, ... */
};
function getOldestNews () {
/* code supposed to return the oldest news */
}
}
]);
getOldestNews
is declared as a local function since there is no point to expose it in the $scope
.
How should I deal with it? How can I actually test this function?
describe("NewsFeedController", function () {
beforeEach(module("app"));
var $controller, $scope, controller;
beforeEach(inject(function (_$controller_) {
$controller = _$controller_;
$scope = {};
controller = $controller("NewsFeedController", { $scope: $scope });
}));
it("should return the oldest news", function () {
// How do I test getOldestNews?
});
});
By the way, it'd be great if the solution also works for local functions within services and directives.
Related questions: