0

Unit test a javascript function in an angular service using jasmine

This is my angular service:

angular.module("app.services").service("validationService", ["$q", function ($q) {

        this.validate = function (filter): ng.IPromise<any> {
            let defer = $q.defer();

            defer.resolve(validateOrder(filter.valA, filter.valB);

            return defer.promise;
        };

        function validateOrder = (valueA, valueB) {
            return valueA > valueB;
        };
    }]);

This is my current unit test:

"use strict";

describe("Service: validationService -> ", function () {
    // load the service"s module
    beforeEach(module("app.services"));

    var validationService;

    beforeEach(inject(function ($injector) {
        validationService = $injector.get("validationService");
    }));

    it("should call validateOrder and return false", function() {
        // Arrange
        var filter = {"valA": 10, "valB": 100};

        // Act and Assert
        expect(validationService.validateOrder(filter.valA, filter.valB)).toEqual(false);
    });

});

I want to test the function called: "validateOrder" within converting it to a this.validateOrder function.

How would I do this while keeping it as a function?

AngularM
  • 15,982
  • 28
  • 94
  • 169

1 Answers1

-1

I don't think you can test private functions without exposing them. Public function(functions on scope) you test should intern test private functions.

How to test 'private' functions in an angular service with Karma and Jasmine

Community
  • 1
  • 1
Ravi Teja
  • 1,097
  • 8
  • 13