私は以下のコードで少し演奏していて、問題を見つけることができませんでした。テストは失敗しました。未定義。"コントローラと約束事で動作していない角度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つのテスト作業。助けてください?
理由は、我々は[MCVE]だけでなく、他のいくつかのサイトで、質問自体に含まれることを期待していることです。 –
私のコメントの一部を繰り返してみましょう:**他のサイトだけでなく、質問自体にも**含まれています** –
、ありがとう、ありがとう。さて、私は何度もそれをチェックし、解決策を探して、私はそれを行うためのルではありませんでした。 – MarcosF8