2016-12-12 6 views
1
angular.module('MyApp') 
    .controller('loginCtrl', function($scope, $rootScope, $location, $window, $auth) { 
    $scope.login = function() { 
     $auth.login($scope.user) 
     .then(function(response) { 
      if(response.data.users.status === 'success'){ 
      $rootScope.currentUser = response.data.username; 
      $window.localStorage.user = JSON.stringify(response.data.users); 
      $location.path('/'); 
      } 
     }).catch(function(response){ 
      if(response.data.users.status === 'error'){ 
      $scope.error = response.data.users.error; 
      } 
     }) 
    }; 
    }); 

私はエラーを取得する:

users is undefined error 

カルマとジャスミンとコードの上のテスト。

スペックは以下の通りです:

describe('LogController', function() { 

    var $scope, controller, $location, $window; 

    beforeEach(module('MyApp')); 

    beforeEach(inject(function($rootScope, $controller, _$location_ , _$window_ , _$httpBackend_) { 
    $scope = $rootScope.$new(); 
    $window = _$window_ 
    $httpBackend = _$httpBackend_; 
    $location = _$location_; 
    controller = $controller('loginCtrl', { 
     $scope: $scope, 
     $location: $location, 
     $window: $window 
    }); 
    })) 

    it('should log in a user and redirect to homepage', function() { 

    $httpBackend.when('POST','/user/auth').respond(200); 

    //whitelist all view 
    $httpBackend.whenGET(/partials.*/).respond(200, ''); 
    $scope.username = 'test'; 
    $scope.password = '123456'; 
    $scope.login(); 

    $httpBackend.flush(); 
    $scope.$apply(); 
    expect($location.path()).toBe('/x'); 
    }); 

}) 

未定義usersは何ですか?そして、私はどのようにして$window.localStorageからの回答を模倣するのですか?

+0

エラー、コードを、と期待される結果は、同様にあなたの質問にする必要があります。あなたが見る必要がある情報を投稿するためにペーストビンを使用することはできません。 –

+0

@GeorgeStockerさん、ありがとうございます。でも、これに対する答えがありますか?私は何時間も立ち往生している。 –

+0

あなたの虚偽の応答に 'data'オブジェクトを含める必要があります。現在、空の文字列を返しています:' $ httpBackend.whenGET(/ partials。* /)。respond(200、 ''); ' 。この空文字列はデータオブジェクトでなければなりません: '{data:{" users ":{" status ":" Worked "}}}' –

答えて

0

はYoushouldはログイン $httpBackend.when('POST','/user/auth').respond(200, mockResponseData);

の応答モック:

mockResponseData = { "data": { "username": "someValue", "users": { "status": "success" } } }

関連する問題