2017-06-09 10 views
0

私は以下のコードで少し演奏していて、問題を見つけることができませんでした。テストは失敗しました。未定義。"コントローラと約束事で動作していない角度ng-mockのジャスミンテスト

私は私のコントローラに約束を返すサービスを持っています。コントローラーでは、私は$ q.allを使用して、約束が解決された直後にいくつかのことをやっています。

私はこの例に従おうとしましたが、大きな違いは、この例ではコントローラのルートにコールがあり、メソッドコール "$ scope.CustomerTest"の中にサービスコールがあることです適用する前に、この追加のラインを持っている($の範囲を$が適用されます()。) "$ scope.CustomerTest( '氏は');":

var $scope; 
var $q; 
var deferred; 
var $httpBackend; 

//Inject the module "before each test" 
beforeEach(angular.mock.module('marketingApp')); 

beforeEach(inject(function ($controller,_$httpBackend_, _$rootScope_, _$q_, marketingService) { 
    $q = _$q_; 
    $scope = _$rootScope_.$new(); 
    $httpBackend = _$httpBackend_; 

    // We use the $q service to create a mock instance of defer 
    deferred = _$q_.defer(); 

    // Use a Jasmine Spy to return the deferred promise 
    spyOn(marketingService, 'getTitleSuggested').and.returnValue(deferred.promise); 

    // Init the controller, passing our spy service instance 
    $controller('customerController', { 
     $scope: $scope, 
     marketingService: marketingService 
    }); 
})); 

it('should resolve promise', function() { 
    // Setup the data we wish to return for the .then function in the controller 
    var titles = [{ "Id": 1, "Name": "Mr" }]; 

    deferred.resolve(titles); 

    $httpBackend.when('GET', '/MarketingCustomers/GetTitleSuggested') 
     .respond(200, titles); 

    //I call to the controller method here. 
    $scope.CustomerTest('Mr'); 

    $scope.$apply(); 

    // Since we called apply, not we can perform our assertions 
    //expect($scope.TitlesTest).not.toBe(undefined); 
    expect($scope.SelectedCustomerTitle).toEqual('Mr'); 
    //expect($scope.error).toBe(undefined); 
}); 

http://www.bradoncode.com/blog/2015/07/13/unit-test-promises-angualrjs-q/

これは私のテストコードです

これはプランナーです:

http://plnkr.co/edit/3IMzqH1yKW8kazZFWaA0?p=preview 

コントローラの最初のテスト(it)にコメントする.sps.js他の2つのテスト作業。助けてください?

+0

理由は、我々は[MCVE]だけでなく、他のいくつかのサイトで、質問自体に含まれることを期待していることです。 –

+0

私のコメントの一部を繰り返してみましょう:**他のサイトだけでなく、質問自体にも**含まれています** –

+0

、ありがとう、ありがとう。さて、私は何度もそれをチェックし、解決策を探して、私はそれを行うためのルではありませんでした。 – MarcosF8

答えて

0

誰かが同じような問題を抱えている場合は、ここでも問題は解決しました。うまくいけば、それは役に立ちます。ここ

試験である:

describe('CustomerController.js', function() { 

var results = [{ 
"Id": 1, 
"Name": "Mr" 
}]; 

var $controller; 
var $q; 
var $rootScope; 
var $scope; 
var marketingService; 

beforeEach(angular.mock.module('marketingApp')); 

beforeEach(inject(function(_$rootScope_, $controller, _$q_, 
_marketingService_) { 
$scope = _$rootScope_.$new(); 
$q = _$q_; 
$rootScope = _$rootScope_; 
marketingService = _marketingService_; 

$controller('customerController', { 
    $scope: $scope, 
    $rootScope: $rootScope 
}); 

spyOn(marketingService, 'getTitleSuggested').and.callFake(function() { 
    var deferred = $q.defer(); 
    deferred.resolve(results); 
    return deferred.promise; 
}); 

})); 

it('It should call the service" ', function() { 

$scope.CustomerTest('Mr'); 
expect(marketingService.getTitleSuggested).toHaveBeenCalledWith('Mr'); 
}); 


it('It should populate the "$scope.TitlesTest" ', function() { 

$scope.CustomerTest('Mr'); 
$rootScope.$apply(); 

expect($scope.hello).toEqual('Hello Mr Marcos'); 
expect($scope.Titles[0].Name).toBe('Mr'); 
expect($scope.Titles).toBe(results); 
}); 

})。

、これはplunkerです:あなたは、通常のリンクを追加することができませんでした http://plnkr.co/edit/3IMzqH1yKW8kazZFWaA0?p=preview

関連する問題