Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting- trying to get user JSON object by username using $httpBackend.expectGET
    primarykey
    data
    text
    <p>I'm new to Angular and Jasmine and I'm trying to learn testing with Angular and Jasmine and am having trouble getting the $httpBackend.expectGet to return my JSON object. I've got a basic test below. Can someone please tell me what I'm doing wrong?Here is my code:</p> <pre><code>'use strict'; /* jasmine specs for services go here */ var userData = { id:1, firstName:"John", lastName:"Doe", username:"jdoe", email:"John.Doe@blah.com", phone:"5555551456", password:"changeme" } /*beforeEach(inject(function(_UserService_, $injector) { UserService = _UserService_; $httpBackend = $injector.get('$httpBackend'); $httpBackend.expect('GET', '/daf/rest/user/').respond(userData); }));*/ //TODO: isn't properly stubbing the http return data describe('Service: UserService', function() { var UserService, $httpBackend; beforeEach(function() { module('cdicms.services.user'); inject(function(_$httpBackend_, _UserService_) { $httpBackend = _$httpBackend_; UserService = _UserService_; }); }); it('should not be undefined', function() { expect(UserService).toBeDefined(); }); it('should not be undefined', function() { expect($httpBackend).toBeDefined(); }); it('should get user json based on username', function() { var user; var username = "jdoe"; $httpBackend.expectGET('/daf/rest/user/').respond(userData); //dump(username); user = UserService.getUser(username); //dump(user); //expect(user).toEqualData(userData); }); }); </code></pre> <p>Here is the services code:</p> <pre><code>var userServices = angular.module('cdicms.services.user', []); userServices.factory('UserService', function($http) { var service = {}; var User = function() { this.firstName = ''; this.lastName = ''; this.userName = ''; this.emailAddress = ''; this.phoneNumber = ''; this.password = ''; }; service.getUser = function(username) { var user; $http({method: 'GET', url: '/daf/rest/user/'}). success(function(data, status, headers, config) { user = new User(); user.id = data.id; user.firstName = data.firstName; user.lastName = data.lastName; user.userName = data.username; user.emailAddress = data.email; user.phoneNumber = data.phone; user.password = data.password; }). error(function(data, status, headers, config) { console.log(data); dump(data); }); return user; }; return service; }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload