0

How to create Jasmine unit test for one function in AngularJS service provider. I want to create mock data for myObject and test function getObjectShape() with that mock data as parameter. How to achieve that?

    (function () {
    'use strict';
    angular.module('objectShapes')
            .provider('shapesResolver', shapesResolver);

        function shapesResolver() {

            this.$get = function () {
                return resolver;
            };

            function resolver(myObject) {

                var service = {
                    getObjectShape: getObjectShape
                };

                function getObjectShape() {
                    return myObject.Shape;
                }
            }
        }
})();
2
  • I don't see any dependency here, I don't understand which mock data you're talking about, and your resolver() function does nothing other than declaring a variable and doing nothing with it. So I'm a bit confused. Also, you forgot to post what you tried. Commented Apr 23, 2015 at 6:07
  • I said that I need to create mock data for myObject which is parameter of function resolver. Ok, it was my mistake, it has no dependencies. I need simple structure of unit test for function resolver. Commented Apr 23, 2015 at 6:14

1 Answer 1

2

Here's a skeleton of a test for your service.

describe('shapesResolver service', function() {
    var shapesResolver;

    beforeEach(module('objectShapes'));
    beforeEach(inject(function(_shapesResolver_) {
        shapesResolver = _shapesResolver_;
    }));

    it('should do something, but what?', function() {
        var mockMyObject = {};

        shapesResolver(mockMyObject);

        // shapesResolver doesn't return anything, and doesn't 
        // have any side effect, so there's nothing to test.

        expect(true).toBeTruthy();
    }); 
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.