1

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:

'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);
});
});

Here is the services 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;
});
yankee
  • 38,872
  • 15
  • 103
  • 162

1 Answers1

0

You need to call $httpBackend.flush() when you want to execute the pending requests:

$httpBackend.expectGET('/daf/rest/user/').respond(userData);
user = UserService.getUser(username);
$httpBackend.flush();
dump(user);
expect(user).toEqualData(userData);

UPDATE(based on the new code you showed us): You are doing ajax requests and they are asynchronous. You call the $http.get function and you just see the actual result when your request is processed by the server. When the UserService method returns the user, the server (in this case the httpBackend mock) didn't process the request yet. But $http has a great facility: it allows you to return promises. This object is returned immediatily by the $http method and when the server (or your mock) process the request, the promised is fulfilled. You can see an example right here: Processing $http response in service

I recommend you to read about promises.

Community
  • 1
  • 1
Wagner Francisco
  • 2,210
  • 1
  • 16
  • 15
  • Thank you for the response. For some reason user is still Undefined. I changed expect(user).toEqualData(userData); to expect(user).toBe(userData); and the error in my console is that "expected Undefined to be...and then it lists out my JSON object. Any ideas on why user is undefined? – user2615099 Jul 24 '13 at 18:40
  • I modified my original post to show the contents of the user-services.js file. Is this what you needed? Many Thanks for the help so far. This problem is plaguing our entire team. – user2615099 Jul 25 '13 at 15:22
  • Unfortunately not, by adding return before the $http, I now get a very interesting error message... Chrome 28.0 (Windows) Service: UserService should get user json based on usernam e FAILED Expected { then : Function, success : Function, error : Function } to be { UserObject is in here }. I removed the user object values because proprietary reasons, but still not sure why I'm getting the error – user2615099 Jul 26 '13 at 16:12
  • Hi, sorry, I made a mistake. I thought $http services worked like $resource (with $resource you can do what I mentioned before). I edited my answer and have put a link with the correct way to work with async services. – Wagner Francisco Jul 29 '13 at 12:18
  • Thank you. I'll check it out and get back to you! – user2615099 Jul 30 '13 at 15:32
  • Thanks for all the help on this. We are making some progress! – user2615099 Aug 07 '13 at 14:21