2015-10-05 169 views
0

我正在爲登錄模塊編寫一系列單元測試。目前,所有的失敗都在按預期應用。但是,我在測試有效登錄時遇到問題,因爲測試返回有效登錄測試的錯誤Could not resolve 'models' from state ''角度ui路由器測試狀態

給定一個身份驗證服務,在返回消息從服務器獲取和轉移

angular.module('qServices').service('qsAuthentication', function($resource, qsSession, 
       REST_ROOT, $state){ 

    var Session = $resource(REST_ROOT + 'session'); 

    this.login = function(credentials, errorHandler){ 

     //does a POST 
     Session.save({}, credentials, function(response){ 
      if (response.success === true){ 
       qsSession.refresh(); 
       $state.go('models'); //TODO: "default" state 
      } 

和國家在

angular.module('qRouting', ['ui.router']) 
    .config(function($stateProvider){ 

    $stateProvider.state('models', { 
     parent: 'base', 
     templateUrl: 'modules/models/models-view.html', 
     controller: 'qcModels', 
     url: '/models' 
    }) 

如此定義,基本上就是在這裏發生的,如果response.success是真的,狀態轉換到/models URL並加載合適的模板。不幸的是,測試是在服務

$state.go("models"); 

線返回錯誤Could not resolve 'models' from state ''的測試

describe("Login Test: ", function() 
{ 
    var $httpBackend, 
     rootScope, 
     scope, 
     qsAuthentication, 
     loginError, 
     REST_ROOT, 
     state, 
     templateCache; 

    beforeEach(module("qLogin")); 

    beforeEach(inject(function($injector) 
    { 
     $httpBackend = $injector.get("$httpBackend"); 
     REST_ROOT = $injector.get("REST_ROOT"); 
     state = $injector.get("$state"); 
     qsAuthentication = $injector.get("qsAuthentication"); 
     rootScope = $injector.get("$rootScope"); 
     scope = rootScope.$new(); 
     templateCache = $injector.get("$templateCache"); 

     scope.credentials = { 
      username : "dummytest", 
      password : "dummypass" 
     }; 

     loginError = function(errorMessage) 
     { 
      scope.loginErrorMessage = errorMessage; 
     }; 

     templateCache.put('modules/models/models-view.html'); 
    })); 

    it("Should send a valid login request", function() 
    { 
     $httpBackend.expectPOST(REST_ROOT + "session", scope.credentials) 
      .respond(200, {success : true}); 
     qsAuthentication.login(scope.credentials, loginError); 
     rootScope.$apply(); 
     $httpBackend.flush(); 

     expect(state.current.name).toBe("models"); 
    }); 
}; 

錯誤發生。我該如何解決這個問題?

回答

1

我想,我們正在面對「缺少模塊依賴」這裏。

的一點是,該模塊qLogin這將是測試:

// this module will be tested 
beforeEach(module("qLogin")); 
... 

與狀態'models'工作 - 在其他模塊定義'qRouting'

angular.module('qRouting', ['ui.router']) 
    .config(function($stateProvider){ 

     $stateProvider.state('models', { 
     ... 

雖然我們看不到模塊'qLogin'聲明(二傳),我所期望的,它缺少的依賴到模塊'qRouting'

// module 'qRouting' is not having access to qLogin 
angular.module('qRouting', ['ui.router']) 

所以,這裏的解決方案應擴大該模塊聲明

// now we have state 'models' in the test domain 
angular.module('qRouting', ['ui.router', 'qLogin']) 
+0

這是有道理的。看起來我們對路由狀態和功能模塊有一些重構。確切地說,是 – Jason

+0

。老實說,我不得不說,在我們的應用程序 - 這個國家 - 是減少模塊數量的原因。事實上,對於應用程序,我們只有一個。一些其他人爲真正孤立的組件... –