如何使用karma / jasmin模拟和单元测试以下AngularJS工厂

时间:2016-08-01 12:25:43

标签: angularjs unit-testing mocking karma-jasmine

I have an angular factory which again calls another factory-  service 
that uses $resource for API call. 
Could someone tell me, How can I mock and test this kind of  constructs.

//factory
     angular.module('myapp')
        .factory('SampleF', function (SampleS) {
            return {
                getData: function (parm, callback) {
                    var cb = callback || angular.noop;

                    return SampleS.get(parm,
                        function (res) {
                            return cb(res);
                        },
                        function (err) {
                            return cb(err);
                        }.bind(this)).$promise;
                }
            };
        });


// Service: SampleS 

       angular.module('myapp')
        .factory('SampleS', function ($resource) {
            return $resource('http://localhost:8080:/api/sample/:parm', {
                parm: '@parm',
            }, {});
        });

//API response will be 
`          {
            "firstName": "John",
            "lastName": "Franklin",
            "companyName": "Benton, John B Jr",
            "address": "6649 N Blue Gum St",
            "city": "New Oakland",
            "county": "Oakland",
            "statie": "LA",
            "zip": "703333",
            "phone": "503-321-2227",
            "phone2": "514-145-427",
            "email": "john@gmail.com",
         }

1 个答案:

答案 0 :(得分:0)

describe('test',function(){

    beforeEach(module('myapp'));
    var sampleF,myapp,
    beforeEach(inject(function(_sampleF_,_myapp_){
        sampleF=_sampleF_;
        myapp=_myapp_;
    }));
    it('testing'function(){
        //here you can call your function and expect the values
    });
 });