AngularJS - Block Test Factories with Complex Inner Logic

I am currently writing unit tests for my angularjs application and would like some advice on how to test my factories. Unfortunately, since I wrote all the code before writing the tests, I have some pretty complex logic in my factories.

At the moment, for example, I have a httpRequest service:

angular.module('myApp')
    .service('httpRequest', ['$http',
        function($http) {
            function constructHttpRequest() {/*code*/}
            function constructCertainTypeOfHttpRequest() {/*code*/}
            function constructOtherTypeOfHttpRequest() {/*code*/}

            return {
                method1: function() {/*code*/}
                method2: function() {/*code*/}
                method3: function() {/*code*/}
            }
        }]);

      

The service provides many different request methods that use rather complex functions construct...

(which are not displayed) to generate requests.

Ideally, I would test all functions construct...

and then verify that the methods call those functions with the correct parameters; I really don't want to copy and paste tests for logic construct...

.

My question is, is there any neat way to expose these functions for testing, but not expose them to the entire application? In a perfect world, how would I write the factory code; should I put the functions construct...

in a separate service and just think about never accessing them directly?

+3


source to share


1 answer


Have you tried splitting the logic in your design into different services and then forcing those services to inject those services into your service httpRequest

? This will allow you to write a test for the injected services once and not rewrite them for each build function. Also, take a look at $ resourceProvider . $resourceProvider

usually covers everything you need if you are building something based on a RESTful API. Hope it helps.



0


source







All Articles