jeudi 18 mai 2017

New to testing in angularjs / jasmine. What can be tested in this object file? I have a few tests

Hey I recently took an internship and I have been given the task to write test cases for objects in angularjs, & jasmine / karma. I have been able to come up with a few tests for each file, which I have put below, but I am not sure if this is all that can be tested. Any help would be greatly appreciated.

It is also my understanding that you cannot test constructors or private methods. Is this correct? Another object has a big if/else statement as well. Can that be tested?

So far this has been all that I can come up with. Would this suffice as a "good" test or is there other things that I should be testing? How would I go about it?

Object File

.factory('CommonQuestions', ['common_questions_factory', 'Form', '$rootScope',
    function (common_questions_factory, Form, $rootScope) {
    // Ctor
    function CommonQuestions(data) {
        var keys = Object.keys(data);
        for (var i = 0; i < keys.length; i++) {
            this[keys[i]] = data[keys[i]];
        }
    };

    CommonQuestions.prototype.Select = function () {
        this.Id = guid();

        Form.CurrentForm().AddCommonQuestion(angular.copy(this));
    };
    CommonQuestions.prototype.Remove = function () {
        common_questions_factory.delete(this.Id).then(function () {
            window.location.reload();
        });
    };

        // Static Methods
    CommonQuestions.Current = function () {
        return $rootScope.config_info;
    };
    CommonQuestions.GetAll = function (callback) {
        common_questions_factory.get().then(function (data) {
            var collection = [];

            for (var i = 0; i < data.length; i++) {
                collection.push(new CommonQuestions(data[i]));
            }

            callback(collection);
        });
    };

    return CommonQuestions;
}]);

Test File

/*Object Defined*/
it('should have CommonQuestions be defined', function () {
    expect(CommonQuestions).toBeDefined();
});

/*Object.Current*/
it('should call CommonQuestions.Current', function () {
    CommonQuestions.Current();
    expect(CommonQuestions.Current).toHaveBeenCalled();
});

/*factory.get*/
it('should have a function get', function () {
    expect(typeof common_questions_factory.get).toBe('function');
});

Aucun commentaire:

Enregistrer un commentaire