D
D
Denis Denis2016-01-18 14:03:37
JavaScript
Denis Denis, 2016-01-18 14:03:37

How to make a stub service for tests?

Once again, hello everyone.
I’ll say a big thank you to everyone right away for their help :)
There is an Angular controller

taskApp.controller('taskListCtrl', function ($scope, $rootScope, taskService) {

    taskService.getTaskList().then(function (data) {
        $rootScope.tasks = $scope.tasks = data.data;
    });
});

As I understand it, in order to test this controller, you need to make a stub for the taskService service, which will have a then property that will return any string (as an example), and the test task will check that the controller has written data to the scope.
Point the bastard.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-01-18
@golovewkin

1) don't use $scope in controller. Y031 . And especially don't use $rootScope. Also, don't use both.
2) if we complete the previous step, we will have 2 less controller dependencies, and we only need to check the controller state. In this case, for the test, it’s enough just to take our function and replace the dependencies through mocks (if you use jasmine, then everything is already there, if you use mocha, then I recommend sinon)
3) Advanced ownership: Don't use ngController or state/route controllers (maximum for pushing data from resolvers to scope). Crush everything into directives. Moreover, pass the data into them from above through the bindings and do not request directly from the inside through the services. Then there will be no need to test anything in our UI component at all. Enough to check the bindings. And the model will already be covered with regular tests, and our model will be completely separated from angular (services like $http and the dependency container do not count).
ps an example of a simple application where all three points are: here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question