How to build a simple http script test post using angular js

I am just starting to understand Angularjs and plan to create an application. I am really a PHP programmer and have a little background in javascript. Angularjs was introduced to me by a friend. I was warned that I should also study its testing of Jasmine / karma before the functionality of the application becomes larger. So, at the moment I have a message in http format that sends an email and password, which, if successful, will return the token. Basically, if success redirects 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 testcript:

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

Failed to run test test :(. 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 

Someone can help. So sorry for the trouble though.

+4
source share
1 answer

So this is because Api.authenticatePlayer calls a different path than you expect.

Instead, your test should have this:

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

Basically, in your test, $httpBackend is a mock code that will call your API. You can say, “When my code calls this url, answer“ _. ”In this code you say you expect the message to happen and return a blank 200 response. You can replace“ 200 ”with the json payload that you want to pretend that the server responded.

+2
source

Source: https://habr.com/ru/post/1500138/


All Articles