Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm just beginning to understand Angularjs and planning to build an app. I'm really a PHP programmer and have little background in javascript. Angularjs was introduced to me by a friend. I was warned that I have to also learn its Jasmine/karma testing before the functionality of the app gets bigger. So here goes, for now I have a $http post which submits an email and a password which if success return a token. Basically if success will redirect the user to the user/profile page

Controller code:

function MainCtrl($scope, $location, Api, localStorageService, Security) {
 $scope.loginUser = function () {
    Api.authenticatePlayer({
        email    : $scope.main.email,
        password : $scope.main.password
    }).then(function (result){
        //success
        $location.path('/user/profile');
    }, function(result) {
        //error also this will catch error 400, 401, and 500
        console.log(result.data);
    });
 };
}

And here is my testscript:

beforeEach(function() {
    module('myApp.services'),
    module("myApp.controllers")
});

beforeEach(inject(function($controller, $rootScope, $location, Api, localStorageService, $httpBackend, Security) {
    this.$location = $location;
    this.$httpBackend = $httpBackend;
    this.scope = $rootScope.$new();
    this.redirect = spyOn($location, 'path');

    $controller("MainCtrl", {
        $scope : this.scope,
        $location : $location,
        localStorageService : localStorageService,
        Security : Security
    });

}));

describe("successfully logging in", function () {
    it("should redirect you to /user/profile", function() {
        //arrange
        var postData = {
            email : this.scope.main.email,
            password : this.scope.main.password
        }
        this.$httpBackend.expectPOST('login', postData).respond(200);
        //act
        this.scope.loginUser();
        this.$httpBackend.flush();
        //assert
        expect(this.redirect).toHaveBeenCalledWith('/user/profile');
    });
});

Here is my service.js code:

return {

  /**
   * Authenticate player
   * @param   object postData      Email and password of the user
   * @return object
   */
  authenticatePlayer: function(postData) {
    return $http({
      method  : 'POST',
      url     : api + 'auth/player',
      data    : postData,
      headers : {'Content-Type' : 'application/json'}
    });
  }
 }

The testscript failed :(. Here is the error:

Chrome 24.0 (Linux) controller: MainCtrl successfully logging in should redirect you to /user/profile FAILED
Error: Unexpected request: POST http://domain.com/auth/player
Expected POST login

Can anyone please help. So sorry for the trouble though.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

So, this is because Api.authenticatePlayer is calling to a different path than what you are expecting.

Your test should have this instead:

this.$httpBackend.expectPOST('http://domain.com/auth/player', postData).respond(200);

Basically, in your test, $httpBackend is a mock of the code that would call your API. You get to say "When my code calls this URL, respond with _". In this code, you are saying that you expect the post to happen and to return an empty response of 200. You could replace "200" with the json payload that you want to pretend that the server responded with.

share|improve this answer
    
i updated my question. please take a look above. I've added the authenticatePlayer function. the page where my login reside is in my /home and its partials is sign_in.html. –  user2720708 Sep 2 '13 at 12:28
    
oh, okay I understand a bit now. So if I want to test the response when failed I'll just change the respond(400/401/500) then assert the error result. Am i right? –  user2720708 Sep 2 '13 at 12:37
    
Yes. Completely. That line is all about setting up test responses. You can mock out anything the server might throw at you. –  Brian Genisio Sep 2 '13 at 12:40
    
thanks men. :D. God Bless you –  user2720708 Sep 2 '13 at 12:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.