Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to build a simple $http post test script using angular js
    primarykey
    data
    text
    <p>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</p> <p>Controller code:</p> <pre><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); }); }; } </code></pre> <p>And here is my testscript:</p> <pre><code>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'); }); }); </code></pre> <p>Here is my service.js code:</p> <pre><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'} }); } } </code></pre> <p>The testscript failed :(. Here is the error:</p> <pre><code>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 </code></pre> <p>Can anyone please help. So sorry for the trouble though. </p>
    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