2014-10-29 69 views
0

我是AngularJS的新手,當然還有JS測試。我陷入瞭如何測試角度控制器內部驅動的數據。我瞭解到,sinon可以存儲函數調用(如服務)來獲取僞造的數據。我也知道Angular有$提供...但是因爲我是新的,所以我迷路了。所以我想問你的opions,我做錯了什麼,接下來應該怎麼做才能夠爲我的Angular應用程序正確編寫JS測試。AngularJS使用它的依賴服務測試控制器,該服務可以進行嵌套調用。我被困在使用Sinon Stub

控制器

(function() { 
    'use strict'; 

    var controllerId = 'appCtrl'; 

    angular.module('app').controller(controllerId, 
     ['common', 'datacontext', appCtrlFn]); 

    function appCtrlFn(common, datacontext) { 

     var vm = this; 
     vm.title = 'My Title'; 
     vm.allFooData = []; 
     vm.count = 0; 

     activate(); 

     function activate() { 
      common.activateController([getAllFooData()], controllerId) 
       .then(function() { log('Activated.'); }); 
     } 

     function getAllFooData(forceRefresh) { 
      return datacontext.allFooData.getAll(...) 
       .then(function (data) { 

        vm.allFooData = data; 

        getCount(); 

        return data; 
       } 
      ); 
     } 

     function getCount() { 
      return datacontext.allFooData.getCount() 
       .then(function (num) { 
        return vm.count = num; 
      }); 
     } 
    } 
})(); 

datacontext.allFooData實際上是一個角服務(美孚)

(function() { 
    'use strict'; 

    var serviceId = 'repository.foo'; 

    angular.module('app').factory(serviceId, 
     [fooRepositoryFn]); 

    function fooRepositoryFn() { 

     function ctor() { 

      this.getAllFooData = getAllFooData; 
      this.getCount = getCount; 
     } 

     return ctor; 

     function getAllFooData() { 

      return [ 
       { 
        "id": 1, 
        "name": "foo1", 
       }, 
       { 
        "id": 2, 
        "name": "foo2", 
       }, 
       { 
        "id": 3, 
        "name": "foo3", 
       }, 
      ]; 
     } 

     function getCount() { 

      return 3; 
     } 
    } 
})(); 

我的測試碼

describe("Controller Tests", function() { 

    var controller, 
     controllerName = 'appCtrl', 
     service, 
     serviceName = 'repository.foo'; 

    beforeEach(function() { 

     // Load app module 
     module('app'); 

     inject(function($injector) { 

      // Get the controller 
      controller = $injector.get('$controller')(controllerName); 

      // Get the service 
      service = $injector.get(serviceName); 
     }); 
    }); 

describe("Test case 1", function() { 

     it("Should be created", function() { 

      expect(controller).toBeDefined(); // PASSED 
     }); 

     it("Should have title", function() { 

      expect(controller.title).toEqual('My Title'); // PASSED 
     }); 
    }); 

    describe("Repository Test", function() { 

     it("Should be defined", function() { 

      expect(service).toBeDefined(); // PASSED 
     }); 

     it("Should get data", function() { 

      // Stub the 'getCount' function from the service (is it internal)? 
      sinon.stub(service, 'getCount', function() { // ERROR 

       // I don't want to touch the real data, so I created a fake 
       var count = fooDataMock.getAllFooData().length; // returns 1 
       return count; 
      }); 

      expect(service.getCount).toEqual(1); // FAILED 
     }); 
    }); 
}) 

這是錯誤消息

TypeError: Attempted to wrap undefined property getCount as function 
    at Object.wrapMethod (http://localhost:9876/base/Scripts/sinon-1.11.0.js:739:25) 
    at Object.stub (http://localhost:9876/base/Scripts/sinon-1.11.0.js:2539:26) 
    at null.<anonymous> (http://localhost:9876/base/app/tests/foo/foo.spec.js:70:19) 
    at jasmine.Block.execute (http://localhost:9876/absolute.../npm/node_modules/karma-jasmine/lib/jasmine.js:1145:17) 
    at jasmine.Queue.next_ (http://localhost:9876/absolute.../npm/node_modules/karma-jasmine/lib/jasmine.js:2177:31) 
    at jasmine.Queue.start (http://localhost:9876/absolute.../npm/node_modules/karma-jasmine/lib/jasmine.js:2130:8) 
    at jasmine.Spec.execute (http://localhost:9876/absolute.../npm/node_modules/karma-jasmine/lib/jasmine.js:2458:14) 
    at jasmine.Queue.next_ (http://localhost:9876/absolute.../npm/node_modules/karma-jasmine/lib/jasmine.js:2177:31) 
    at jasmine.Queue.start (http://localhost:9876/absolute.../npm/node_modules/karma-jasmine/lib/jasmine.js:2130:8) 
    at jasmine.Suite.execute (http://localhost:9876/absolute.../npm/node_modules/karma-jasmine/lib/jasmine.js:2604:14) 

回答

0

使用ctor對象和函數調用:

expect(service.ctor.getCount()).toEqual(1) 
相關問題