I have the same exact issue, did you manage to solve it?
Thank you

On Wednesday, 23 October 2013 21:26:33 UTC+1, Grant Harris wrote:
>
> The first test works properly as it returns a 200 status code, but the 
> second two do not.
>
> On Wednesday, October 23, 2013 1:25:19 PM UTC-7, Grant Harris wrote:
>>
>> I'm having some issues with testing non 200 response codes with the spec 
>> unit testing in Angular 1.2. These unit tests used to work, however, after 
>> upgrading from Angular 1.15 to Angular 1.2, the mock http calls that return 
>> a non 200 level response code (In my case 400) stopped working properly in 
>> my unit tests.
>>
>> I get the following errors (see below) when my unit tests 'should error 
>> when passwords do not match' and 'should error when current password is not 
>> correct' return a 400 error. Switching the mocks to return a 200 error 
>> instead of a 400 error resolves this issue. 
>>
>> Am I doing something wrong or is perhaps something broken with the 
>> testing framework and non 200 statuses? Any ideas?
>>
>> Thanks,
>>
>> Grant
>>
>>
>> TypeError: Cannot read property 'data' of undefined
>>
>>     at 
>> /Users/grant/hg/wardenclyffe/manhattan/app/bower_components/angular/angular.js:6311:22
>>
>>     at wrappedCallback 
>> (/Users/grant/hg/wardenclyffe/manhattan/app/bower_components/angular/angular.js:9106:81)
>>
>>     at 
>> /Users/grant/hg/wardenclyffe/manhattan/app/bower_components/angular/angular.js:9192:26
>>
>>     at Scope.$eval 
>> (/Users/grant/hg/wardenclyffe/manhattan/app/bower_components/angular/angular.js:10034:28)
>>
>>     at Scope.$digest 
>> (/Users/grant/hg/wardenclyffe/manhattan/app/bower_components/angular/angular.js:9882:23)
>>
>>     at Scope.$apply 
>> (/Users/grant/hg/wardenclyffe/manhattan/app/bower_components/angular/angular.js:10137:24)
>>
>>     at done 
>> (/Users/grant/hg/wardenclyffe/manhattan/app/bower_components/angular/angular.js:6576:45)
>>
>>     at handleResponse 
>> (/Users/grant/hg/wardenclyffe/manhattan/app/bower_components/angular-mocks/angular-mocks.js:1111:9)
>>
>>     at Function.$httpBackend.flush 
>> (/Users/grant/hg/wardenclyffe/manhattan/app/bower_components/angular-mocks/angular-mocks.js:1439:26)
>>
>>     at null.<anonymous> 
>> (/Users/grant/hg/wardenclyffe/manhattan/test/spec/services/account.js:65:17)
>>
>>
>> Error: [$rootScope:inprog] $digest already in progress
>>
>> http://errors.angularjs.org/1.2.0-rc.2/$rootScope/inprog?p0=%24digest
>>
>>     at 
>> /Users/grant/hg/wardenclyffe/manhattan/app/bower_components/angular/angular.js:78:12
>>
>>     at beginPhase 
>> (/Users/grant/hg/wardenclyffe/manhattan/app/bower_components/angular/angular.js:10328:15)
>>
>>     at Scope.$digest 
>> (/Users/grant/hg/wardenclyffe/manhattan/app/bower_components/angular/angular.js:9874:9)
>>
>>     at Function.$httpBackend.verifyNoOutstandingExpectation 
>> (/Users/grant/hg/wardenclyffe/manhattan/app/bower_components/angular-mocks/angular-mocks.js:1462:16)
>>
>>     at null.<anonymous> 
>> (/Users/grant/hg/wardenclyffe/manhattan/test/spec/services/account.js:20:17)
>>
>>
>> *Here's the code I'm using.*
>>
>> 'use strict';
>>
>> describe('Service: account', function () {
>>
>>   var httpBackend, account, scope;
>>
>>   beforeEach(function() {
>>     module('manhattanApp');
>>
>>     inject(function($httpBackend, $rootScope, _account_) {
>>       httpBackend = $httpBackend;
>>
>>       account = _account_;
>>       scope = $rootScope.$new();
>>     });
>>
>>   });
>>
>>   afterEach(function() {
>>     httpBackend.verifyNoOutstandingExpectation();
>>     httpBackend.verifyNoOutstandingRequest();
>>   });
>>
>>   it('should change the password on success return', function() {
>>     // Setup the fake http response
>>     httpBackend.whenPOST('/api/v1/account/change_password').respond(200, 
>> 'success');
>>
>>     // Setup the spies for testing
>>     var success_callback = jasmine.createSpy('success_callback');
>>     var error_callback = jasmine.createSpy('error_callback');
>>
>>     // Perform the call associating callbacks with spies
>>     var my_response = account.change_password('current_pass', 'new_pass', 
>> 'new_pass');
>>     my_response.success(success_callback);
>>     my_response.error(error_callback);
>>
>>     // Digest to fire the http calls, then flush backend to trigger 
>> callbacks
>>     scope.$digest();
>>     httpBackend.flush();
>>
>>     // Ensure we expect things to happen as they should
>>     expect(success_callback).toHaveBeenCalled();
>>     expect(success_callback.calls.length).toEqual(1);
>>     expect(success_callback.calls[0].args[0]).toEqual('success');
>>     expect(success_callback.calls[0].args[1]).toEqual(200);
>>
>>     expect(error_callback.calls.length).toEqual(0);
>>   });
>>
>>   it('should error when passwords do not match', function() {
>>     // Setup the fake http response
>>     httpBackend.whenPOST('/api/v1/account/change_password').respond(400, 
>> 'Passwords Do Not Match');
>>
>>     // Setup the spies for testing
>>     var success_callback = jasmine.createSpy('success_callback');
>>     var error_callback = jasmine.createSpy('error_callback');
>>
>>     // Perform the call associating callbacks with spies
>>     var my_response = account.change_password('current_pass', 'new_pass', 
>> 'new_pass_no_match');
>>     my_response.success(success_callback);
>>     my_response.error(error_callback);
>>
>>     // Digest to fire the http calls, then flush backend to trigger 
>> callbacks
>>     scope.$digest();
>>     httpBackend.flush();
>>
>>     // Ensure we expect things to happen as they should
>>     expect(success_callback.calls.length).toEqual(0);
>>
>>     expect(error_callback).toHaveBeenCalled();
>>     expect(error_callback.calls.length).toEqual(1);
>>     expect(error_callback.calls[0].args[0]).toEqual('Passwords Do Not 
>> Match');
>>     expect(error_callback.calls[0].args[1]).toEqual(400);
>>   });
>>
>>   it('should error when current password is not correct', function() {
>>     // Setup the fake http response
>>     httpBackend.whenPOST('/api/v1/account/change_password').respond(400, 
>> 'Not Your Current Password');
>>
>>     // Setup the spies for testing
>>     var success_callback = jasmine.createSpy('success_callback');
>>     var error_callback = jasmine.createSpy('error_callback');
>>
>>     // Perform the call associating callbacks with spies
>>     var my_response = account.change_password('bogus_pass', 'new_pass', 
>> 'new_pass');
>>     my_response.success(success_callback);
>>     my_response.error(error_callback);
>>
>>     // Digest to fire the http calls, then flush backend to trigger 
>> callbacks
>>     scope.$digest();
>>     httpBackend.flush();
>>
>>     // Ensure we expect things to happen as they should
>>     expect(success_callback.calls.length).toEqual(0);
>>
>>     expect(error_callback).toHaveBeenCalled();
>>     expect(error_callback.calls.length).toEqual(1);
>>     expect(error_callback.calls[0].args[0]).toEqual('Not Your Current 
>> Password');
>>     expect(error_callback.calls[0].args[1]).toEqual(400);
>>   });
>> });
>>
>>
>>
>> *This is my service itself*
>>
>> 'use strict';
>>
>> angular.module('manhattanApp')
>>   .service('account', function($http, config) {
>>     return {
>>       change_password: function(current_pass, new_pass, again_pass) {
>>         return $http({
>>             method: 'POST',
>>             url: config.API_BASE_URL + '/account/change_password',
>>             data: {
>>               current_pass: current_pass,
>>               new_pass: new_pass,
>>               again_pass: again_pass
>>             }
>>           });
>>       }
>>     };
>>   });
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to